当前位置: 代码迷 >> J2SE >> Socket http请求中文乱码有关问题
  详细解决方案

Socket http请求中文乱码有关问题

热度:81   发布时间:2016-04-23 20:39:49.0
Socket http请求中文乱码问题
各位高手,麻烦帮忙看看这个 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();
}
}

------解决方案--------------------
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);
  相关解决方案