Socket http请求中文乱码问题

goddy2014 2014-06-10 12:14:54
各位高手,麻烦帮忙看看这个 http请求乱码问题,百思不得其解啊....
Socket http请求"http://www.gome.com.cn/ec/homeus/browse/provinceDroplet.jsp?callback=g"时返回乱码,网页是utf-8,编码的

代码如下:
OutputStream os = null;
BufferedReader br = null;
Socket s = null;
try {
StringBuffer req = new StringBuffer();
req.append("GET /ec/homeus/browse/provinceDroplet.jsp?callback=g HTTP/1.1\r\n");
req.append("Accept: text/javascript, application/javascript, */*\r\n");
req.append("Accept-Language: zh-cn\r\n");
req.append("User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)\r\n");
req.append("Host: www.gome.com.cn\r\n");
req.append("Connection: Close\r\n");
req.append("\r\n");
s = new Socket("www.gome.com.cn",80);
os = s.getOutputStream();
br = new BufferedReader(new InputStreamReader(s.getInputStream(),"utf-8"));
os.write(req.toString().getBytes());
os.flush();
String tmp = "";
StringBuffer sbRespon = new StringBuffer();
while((tmp = br.readLine())!=null){
sbRespon.append(tmp + "\r\n");
}
System.out.println(sbRespon.toString());
} catch (IOException e) {
try {
if (br != null) br.close();
if (os != null) os.close();
if (s != null) s.close();
} catch (IOException e2) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
...全文
496 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_33427754 2016-01-23
  • 打赏
  • 举报
回复
lz问题解决没有,我正在找此回答呢,教下我,谢谢;150255545@qq.com
vnvlyp 2014-06-10
  • 打赏
  • 举报
回复
话说为什么不直接用HttpClient或者下面这样用HttpURLConnection,直接用Socket很麻烦的。。
	public static void main(String[] args) throws IOException {
		URL url = new URL("http://www.gome.com.cn/ec/homeus/browse/provinceDroplet.jsp?callback=g");
		HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		
		connection.connect();
		InputStream in = connection.getInputStream();
		
		GZIPInputStream gzip = new GZIPInputStream(in);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		byte[] buf = new byte[1024];
		int n;
		while ((n = gzip.read(buf)) != -1) {
			baos.write(buf, 0, n);
		}
		
		String content = new String(baos.toByteArray(), "UTF-8");
		System.out.println(content);
		
		connection.disconnect();
	}
vnvlyp 2014-06-10
  • 打赏
  • 举报
回复
Web服务器都是有压缩的,而且大多数服务器就算你不指定压缩算法,它也不会发送未压缩的原文,你这个服务器也不例外,它默认采用GZIP压缩算法,在它返回的Header里写得很清楚了,Content-Encoding: gzip,所以你需要用GZIPInputStream来解压缩。下面代码我试了一下没问题。 屏幕输出: HTTP/1.1 200 OK Server: Tengine Date: Tue, 10 Jun 2014 06:35:56 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: close Content-Encoding: gzip X-ATG-Version: version=QVRHUGxhdGZvcm0vMTAuMC4zcDM= Content-Language: zh-CN X-Powered-By: Servlet/2.5 JSP/2.1 X-Cache: HIT from proxy Set-Cookie: BIGipServerpool_ATG_nginx=3591780874.20480.0000; path=/ p3p: CP="NON DSP COR CURa ADMa DEVa TAIa PSAa PSDa IVAa IVDa CONa HISa TELa OTPa OUR UNRa IND UNI COM NAV INT DEM CNT PRE LOC" g({"citys":[{"city":"北京","id":"11000000"},{"city":"天津","id":"12000000"},{"city":"河北省","id":"13000000"},{"city":"山西省","id":"14000000"},{"city":"内蒙古","id":"15000000"},{"city":"上海","id":"21000000"},{"city":"浙江省","id":"22000000"},{"city":"江苏省","id":"23000000"},{"city":"安徽省","id":"24000000"},{"city":"福建省","id":"25000000"},{"city":"山东省","id":"26000000"},{"city":"广东省","id":"31000000"},{"city":"广西","id":"32000000"},{"city":"海南省","id":"33000000"},{"city":"湖北省","id":"41000000"},{"city":"湖南省","id":"42000000"},{"city":"河南省","id":"43000000"},{"city":"江西省","id":"44000000"},{"city":"黑龙江省","id":"51000000"},{"city":"吉林省","id":"52000000"},{"city":"辽宁省","id":"53000000"},{"city":"宁夏","id":"61000000"},{"city":"新疆","id":"62000000"},{"city":"青海省","id":"63000000"},{"city":"陕西省","id":"64000000"},{"city":"甘肃省","id":"65000000"},{"city":"四川省","id":"71000000"},{"city":"云南省","id":"72000000"},{"city":"贵州省","id":"73000000"},{"city":"重庆市","id":"74000000"},{"city":"西藏","id":"75000000"},{"city":"台湾省","id":"81000000"},{"city":"香港","id":"82000000"},{"city":"澳门","id":"83000000"},{"city":"钓鱼岛","id":"84000000"}]})
	public static void main(String[] args) throws Exception {
		httpRequestTest();
	}
	
	public static String readLine(InputStream in, String charset) 
			throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		int b;
		while ((b = in.read()) != -1 && b != 0x0D) {
			baos.write(b);
		}
		
		if (b != -1) {
			in.read();	// should always be 0x0A
		}
		
		return new String(baos.toByteArray(), charset);
	}
	
	public static void httpRequestTest() throws IOException {
		StringBuilder sb = new StringBuilder();
		sb.append("GET /ec/homeus/browse/provinceDroplet.jsp?callback=g HTTP/1.1\r\n");
		sb.append("Accept: */*\r\n");
		sb.append("Host: www.gome.com.cn\r\n");
		sb.append("Connection: Close\r\n\r\n");
		
		Socket socket = new Socket("www.gome.com.cn", 80);
		OutputStream out = socket.getOutputStream();
		out.write(sb.toString().getBytes());
		
		InputStream in = socket.getInputStream();
		
		String line;
		while ((line = readLine(in, "UTF-8")).length() != 0) {
			System.out.println(line);
		}
		readLine(in, "UTF-8"); // should always be an empty line
		
		GZIPInputStream gzip = new GZIPInputStream(in);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		byte[] buf = new byte[1024];
		int n;
		while ((n = gzip.read(buf)) != -1) {
			baos.write(buf, 0, n);
		}
		
		String content = new String(baos.toByteArray(), "UTF-8");
		System.out.println(content);
		
		socket.close();
	}
goddy2014 2014-06-10
  • 打赏
  • 举报
回复
换其他编码的也不行
oh_Maxy 2014-06-10
  • 打赏
  • 举报
回复
InputStreamReader里的utf-8缓存gbk、gb2312或者不输入试试。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧