apache fluent 乱码
2016-05-06 15:12
1831 查看
http://stackoverflow.com/questions/17990684/how-to-use-the-fluent-api-of-apache-httpclient-to-read-utf-8-coded-website服务器返回的信息没有明确指定内容的编码集,因此HttpClient强制使用ISO-8859-1对内容进行编码,而不是UTF-8。不幸的是只能使用客户化的responsehandler重写默认字符集。注:使用addHeader("Content-Type","text/html;charset=utf-8")无法解决上面的问题Request.Get(url)//获取数据.execute().handleResponse(//防止中文乱码new ResponseHandler<String>() {@Overridepublic String handleResponse(final HttpResponse response) throws IOException {return EntityUtils.toString(response.getEntity(), Consts.UTF_8);}});----------------------原文--------------------The response message returned by the server for this URI does not explicitly specify the charset of the content. In such cases HttpClient is forced to use the default charset encoding for HTTP content, which is
ISO-8859-1andnot
UTF-8.Unfortunately the only way to override the default content charset used by fluent API is by using a custom response handler
ResponseHandler<String> myHandler = new ResponseHandler<String>() { @Override public String handleResponse( final HttpResponse response) throws IOException { return EntityUtils.toString(response.getEntity(), Consts.UTF_8); } }; String html = Request.Get("https://kokos.pl/").execute().handleResponse(myHandler); System.out.println(html);
在使用post发送中文时,服务器接受的是乱码,可通过http://www.cnblogs.com/tecfans/p/3577277.html来解决
List<NameValuePair> forms = Form.form().add("account",this.getJsonString(account)).build();String ret = Request.Post(requestUrl).bodyForm(forms, Charset.forName("utf-8")).execute().returnContent().asString();
相关文章推荐
- Apache-dbutils 简介及事务处理
- 【 Ngnix 】配置路径转发至后端Apache多台虚拟主机
- 王高利:在apache2.4上配置awstats后没权限,显示源码问题,报错总结
- Apache实现本地建立泛域名虚拟主机的方法
- Apache本地二级域名配置方法
- RHEL 6下安装Apache
- PHP5.5.15+Apache2.4.10+MySQL5.6.20配置方法分享
- PHP5.5.15+Apache2.4.10+MySQL5.6.20配置方法分享
- apache url 包含特殊字符 404
- RHEL 6下安装Apache
- how to build apache log4cxx 0.10 by Visual Studio 201*
- Apache Flink源码解析之stream-source
- java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
- php+mysql+apache的安装与配置
- 转:apache 的mod-status
- 深入理解Apache Flink核心技术
- 关于apache-commons-fileupload 的一个异常
- apache的common项目如何导入eclipse中
- 使用maven引入Apache poi jar包
- apache 2.4 +php7安装手册