HttpClient 流式读取时中文乱码的解决方法
HttpClient中可以之间使用HttpMethod的getResponseBodyAsString()方法获取返回的内容,在读取之前,如果需要做编码设置可以这样:
HttpMethod get=new GetMethod(url);
httpClient.executeMethod(get);
get.getParams().setContentCharset("GBK");
这么做虽然可以解决中文乱码问题,但是在HttpClient 3.1中这种方法是不推荐的,会发出警告。
2、流式读取
HttpClient推荐使用流式的读取返回内容,如下:
BufferedReader reader=new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
String tmp=null;
String htmlRet="";
while((tmp=reader.readLine())!=null){
htmlRet+=tmp+"\r\n";
}
System.out.println(new String(htmlRet.getBytes(),"GB2312"));
但是经过尝试发现这样会出现中文乱码问题。经过N次实验后,将上面代码修改如下,乱码问题随之解决。
HttpClient httpClient=new HttpClient();
HttpMethod get=new GetMethod(send_url);
try {
httpClient.executeMethod(get);
//System.out.println(get.getResponseBodyAsString());
BufferedReader reader=new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream(),"ISO-8859-1"));
String tmp=null;
String htmlRet="";
while((tmp=reader.readLine())!=null){
htmlRet+=tmp+"\r\n";
}
System.out.println(new String(htmlRet.getBytes("ISO-8859-1"),"GB2312"));
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
get.releaseConnection();
}
发布日期:2021-07-19 12:29:30
浏览次数:10
分类:技术文章
本文共 1300 字,大约阅读时间需要 4 分钟。
1、非流式读取HttpClient中可以之间使用HttpMethod的getResponseBodyAsString()方法获取返回的内容,在读取之前,如果需要做编码设置可以这样:
HttpMethod get=new GetMethod(url);
httpClient.executeMethod(get);
get.getParams().setContentCharset("GBK");
这么做虽然可以解决中文乱码问题,但是在HttpClient 3.1中这种方法是不推荐的,会发出警告。
2、流式读取
HttpClient推荐使用流式的读取返回内容,如下:
BufferedReader reader=new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
String tmp=null;
String htmlRet="";
while((tmp=reader.readLine())!=null){
htmlRet+=tmp+"\r\n";
}
System.out.println(new String(htmlRet.getBytes(),"GB2312"));
但是经过尝试发现这样会出现中文乱码问题。经过N次实验后,将上面代码修改如下,乱码问题随之解决。
HttpClient httpClient=new HttpClient();
HttpMethod get=new GetMethod(send_url);
try {
httpClient.executeMethod(get);
//System.out.println(get.getResponseBodyAsString());
BufferedReader reader=new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream(),"ISO-8859-1"));
String tmp=null;
String htmlRet="";
while((tmp=reader.readLine())!=null){
htmlRet+=tmp+"\r\n";
}
System.out.println(new String(htmlRet.getBytes("ISO-8859-1"),"GB2312"));
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
get.releaseConnection();
}
转载地址:https://blog.csdn.net/jdsjlzx/article/details/8825845 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月17日 06时44分31秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Flink在美团的应用与实践听课笔记
2019-04-27
Java多线程的11种创建方式以及纠正网上流传很久的一个谬误
2019-04-27
JDK源码研究Jstack,JMap,threaddump,dumpheap的原理
2019-04-27
Java使用字节码和汇编语言同步分析volatile,synchronized的底层实现
2019-04-27
javac编译原理和javac命令行的使用
2019-04-27
Unity使用UnityWebRequest实现本地日志上传到web服务器
2019-04-27
Unity使用RenderTexture实现裁切3D模型
2019-04-27
美术和程序吵架,原来是资源序列化格式设置不统一
2019-04-27
Unity iOS接SDK,定制UnityAppController
2019-04-27
Unity iOS接SDK前先要了解的知识(Objective-C)
2019-04-27
记一次iOS闪退问题的定位:NSLog闪退
2019-04-27
Unity打开照相机与打开本地相册然后在Unity中显示照片(Android与iOS)
2019-04-27
无需接入SDK即可在Unity中获取经纬度(Android/iOS),告诉我你的坐标
2019-04-27
Unity获取系统信息SystemInfo(CPU、显卡、操作系统等信息)
2019-04-27
Unity中获取物体的尺寸(size)的三种方法
2019-04-27
Unity中的关节组件和绳子效果的实现
2019-04-27
Unity可视化编程插件: Bolt,可以像UE4的蓝图那样啦
2019-04-27
Android的.dex、.odex与.oat文件扫盲
2019-04-27