java SOCKET接收数据出现乱码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java SOCKET接收数据出现乱码相关的知识,希望对你有一定的参考价值。
2013-04-30 23:15:08,505 WARN com.swf.httpproxy.InnerToServerHandler - POSThttp://qurl.f.360.cn/wdinfo.phpHTTP/1.1
2013-04-30 23:15:08,505 WARN com.swf.httpproxy.InnerToServerHandler - Content-Type: application/octet-stream
2013-04-30 23:15:08,505 WARN com.swf.httpproxy.InnerToServerHandler - User-Agent: Post
2013-04-30 23:15:08,505 WARN com.swf.httpproxy.InnerToServerHandler - Host: qurl.f.360.cn
2013-04-30 23:15:08,505 WARN com.swf.httpproxy.InnerToServerHandler - Content-Length: 298
2013-04-30 23:15:08,505 WARN com.swf.httpproxy.InnerToServerHandler - Pragma: no-cache
2013-04-30 23:15:08,521 WARN com.swf.httpproxy.InnerToServerHandler - "???m ??M???nvf"b?????????<<W?U=j
使用编码UTF-8
HttpURLConnection 接收网络数据出现乱码问题
由于接收的数据经过gZip处理过,所以在接受的时候也要处理,并且加上编码格式(没有会出现部分数据乱码):
具体代码实现如下:
URL ul = new URL(url); HttpURLConnection conn = (HttpURLConnection) ul.openConnection(); conn.connect(); InputStream in = conn.getInputStream(); StringBuffer sb = new StringBuffer(); String readLine = new String(); GZIPInputStream gZipS=new GZIPInputStream(in); InputStreamReader res = new InputStreamReader(gZipS,"UTF-8"); BufferedReader responseReader = new BufferedReader(res); while ((readLine = responseReader.readLine()) != null) { sb.append(readLine); } responseReader.close();
测试问题解决。。
以上是关于java SOCKET接收数据出现乱码的主要内容,如果未能解决你的问题,请参考以下文章
关于socket UDP通信,接收到乱码的问题!!急急急!SOS!