socket取得的响应乱码

pktmalaha 2013-10-25 03:39:51
我的jvm是utf-8的

我用socket请求:http://tech.qq.com/web/it/telerss.xml(我用cpdetector得到的是gb2312)

下面是我的代码:

try {
URL u=new URL("http://tech.qq.com/web/it/telerss.xml");

String host=u.getHost();
String path=u.getPath();
String param=u.getQuery();

try {
Socket s=new Socket(host,80);
s.setKeepAlive(true);//长连接
OutputStream os=s.getOutputStream();

PrintWriter osOut=new PrintWriter(os,true);
//InputStream osIn=s.getInputStream();
BufferedReader osIn=new BufferedReader(
new InputStreamReader(s.getInputStream(),Charset.forName("GB2312")));
osOut.println("GET "+path+"?"+param+" HTTP/1.1");
osOut.println("Host: "+host+"");
osOut.println("User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36");
osOut.println("Accept-Charset: iso-8859-1, unicode-1-1;q=0.8");
osOut.println("Accept-Language: en-us,en;q=0.8");
osOut.println("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
osOut.println("Accept-Encoding: gzip,deflate");
osOut.println("Connection: Close");
osOut.println();
//get response
StringBuilder sb=new StringBuilder();

int c;
while((c=osIn.read())!=-1){
sb.append((char)c);
}
s.close();
System.out.println(sb.toString());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


高手指定一下,在哪一步乱码了?如何解决?
...全文
172 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pktmalaha 2013-10-25
  • 打赏
  • 举报
回复
引用 6 楼 hardycheng 的回复:
[quote=引用 4 楼 pktmalaha 的回复:] 回:hardycheng 为什么人家的rss用gb2312我管不了,我只能操作我的代码,你说的Accept-Charset我试过了不管用
嗯 刚才看错了,那个是PrintOut 跟这个读取没关系 int c; while((c=osIn.read())!=-1){ sb.append((char)c); } 换成 String line ; while((line = osIn.readLine()) != null){ sb.append(line); } 试试呢 [/quote] 也不行呀
hardycheng 2013-10-25
  • 打赏
  • 举报
回复
引用 4 楼 pktmalaha 的回复:
回:hardycheng 为什么人家的rss用gb2312我管不了,我只能操作我的代码,你说的Accept-Charset我试过了不管用
嗯 刚才看错了,那个是PrintOut 跟这个读取没关系 int c; while((c=osIn.read())!=-1){ sb.append((char)c); } 换成 String line ; while((line = osIn.readLine()) != null){ sb.append(line); } 试试呢
pktmalaha 2013-10-25
  • 打赏
  • 举报
回复
引用 1 楼 a470577391 的回复:
GB2312改成UTF-8呢?
对,我在utf8下输出我拿到的响应,我用的网站试过了,一个html是gb2312编码,我上面的代码可以输出中文且不乱码
pktmalaha 2013-10-25
  • 打赏
  • 举报
回复
回:hardycheng 为什么人家的rss用gb2312我管不了,我只能操作我的代码,你说的Accept-Charset我试过了不管用
hardycheng 2013-10-25
  • 打赏
  • 举报
回复
引用 2 楼 hardycheng 的回复:
看一下 (1) osOut.println("Accept-Charset: iso-8859-1, unicode-1-1;q=0.8"); (2) 还有一个 xml都是 utf-8编码的 你用gb2312去取有可能就有问题 (3) 按字节读取 这三种方式都试一下
osOut.println("Accept-Charset: iso-8859-1, unicode-1-1;q=0.8"); 把 is0-8858-1 换成 gb2312试试
hardycheng 2013-10-25
  • 打赏
  • 举报
回复
看一下 (1) osOut.println("Accept-Charset: iso-8859-1, unicode-1-1;q=0.8"); (2) 还有一个 xml都是 utf-8编码的 你用gb2312去取有可能就有问题 (3) 按字节读取 这三种方式都试一下
白开水MD5 2013-10-25
  • 打赏
  • 举报
回复
GB2312改成UTF-8呢?

62,628

社区成员

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

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