源:http://www.blogjava.net/thinkbase/archive/2006/10/31/HttpClientPOST_UTF8.html
评:
Apache HttpClient ( http://jakarta.apache.org/commons/httpclient/ ) 是一个纯 Java 的HTTP 协议的客户端编程工具包, 对 HTTP 协议的支持相当全面, 更多细节也可以参考IBM 网站上的这篇文章 HttpClient入门 ( http://www-128.ibm.com/developerworks/cn/opensource/os-httpclient/ ).
问题分析
不过在实际使用中, 还是发现按照最基本的方式调用 HttpClient 时, 并不支持 UTF-8 编码, 在网络上找过一些文章, 也不得要领, 于是查看了 commons-httpclient-3.0.1 的一些代码, 首先在 PostMethod 中找到了 generateRequestEntity() 方法:
/**
* Generates a request entity from the post parameters, if present. Calls
* {@link EntityEnclosingMethod#generateRequestBody()} if parameters have not been set.
*
* @since 3.0
*/
protected RequestEntity generateRequestEntity() {
if (!this.params.isEmpty()) {
// Use a ByteArrayRequestEntity instead of a StringRequestEntity.
// This is to avoid potential encoding issues. Form url encoded strings
// are ASCII by definition but the content type may not be. Treating the content
// as bytes allows us to keep the current charset without worrying about how
// this charset will effect the encoding of the form url encoded string.
String content = EncodingUtil.formUrlEncode(getParameters(), getRequestCharSet());
ByteArrayRequestEntity entity = new ByteArrayRequestEntity(
EncodingUtil.getAsciiBytes(content),
FORM_URL_ENCODED_CONTENT_TYPE
);
return entity;
} else {
return super.generateRequestEntity();
}
}
原来使用 NameValuePair 加入的 HTTP 请求的参数最终都会转化为 RequestEntity 提交到 HTTP 服务器, 接着在 PostMethod 的父类 EntityEnclosingMethod 中找到了如下的代码:
/**
* Returns the request's charset. The charset is parsed from the request entity's
* content type, unless the content type header has been set manually.
*
* @see RequestEntity#getContentType()
*
* @since 3.0
*/
public String getRequestCharSet() {
if (getRequestHeader("Content-Type") == null) {
// check the content type from request entity
// We can't call getRequestEntity() since it will probably call
// this method.
if (this.requestEntity != null) {
return getContentCharSet(
new Header("Content-Type", requestEntity.getContentType()));
} else {
return super.getRequestCharSet();
}
} else {
return super.getRequestCharSet();
}
}
解决方案
从上面两段代码可以看出是 HttpClient 是如何依据 "Content-Type" 获得请求的编码(字符集), 而这个编码又是如何应用到提交内容的编码过程中去的. 按照这个原来, 其实我们只需要重载 getRequestCharSet() 方法, 返回我们需要的编码(字符集)名称, 就可以解决 UTF-8 或者其它非默认编码提交 POST 请求时的乱码问题了.
测试
首先在 Tomcat 的 ROOT WebApp 下部署一个页面 test.jsp, 作为测试页面, 主要代码片段如下:
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page session="false" %>
<%
request.setCharacterEncoding("UTF-8");
String val = request.getParameter("TEXT");
System.out.println(">>>> The result is " + val);
%>
接着写一个测试类, 主要代码如下:
public static void main(String[] args) throws Exception, IOException {
String url = "http://localhost:8080/test.jsp";
PostMethod postMethod = new UTF8PostMethod(url);
//填入各个表单域的值
NameValuePair[] data = {
new NameValuePair("TEXT", "中文"),
};
//将表单的值放入postMethod中
postMethod.setRequestBody(data);
//执行postMethod
HttpClient httpClient = new HttpClient();
httpClient.executeMethod(postMethod);
}
//Inner class for UTF-8 support
public static class UTF8PostMethod extends PostMethod{
public UTF8PostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet() {
//return super.getRequestCharSet();
return "UTF-8";
}
}
运行这个测试程序, 在 Tomcat 的后台输出中可以正确打印出 ">>>> The result is 中文" .
代码下载
本文所提到的所有代码, 以及测试程序(可直接导入 eclipse)提供打包下载: att:HttpClient POST 的 UTF-8 编码问题.httpClientUTF8.tar.bz2
END
分享到:
相关推荐
下载这个工具类,然后直接调用createHtmlPage方法就可以了。最关键一点是,网上的方法没有一个解决了乱码问题,费了我不少功夫。
服务器端自己写,本示例为客户端代码,可以解决httpClient4乱码问题,是替代修改源码的最佳写法。
解决HttpClient中文乱码问题jar文件
很全面的代码介绍,很完整的乱码解决说明,让你一目了然,值得研究
HttpClient Post提交多文件及多个普通参数,已经封装成工具类。 需传入 要请求的url 普通参数map 例 map.put("param1","张三"); 需要传入的文件流map 其中key为文件名 服务端接收无乱码。
花了大半天写的测试类,用的时候记得改下引用的包名,可以当util工具类...流方式发送接收: https://blog.csdn.net/akxj2022/article/details/88557826 觉得可以的话,又不想复制粘贴,就花点积分下载吧,o(* ̄︶ ̄*)o
HttpClient4.2.5实现文件上传,无中文文件名乱码问题。亲测无问题。有问题可吐槽!
NULL 博文链接:https://bijian1013.iteye.com/blog/2310211
httpclient中文版本,HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。本文首先介绍 ...
httpclient-4.5.3 api 中文版
从网络获取数据和图片,用gson解析并用listview展示
httpclient4.5的中文帮助文档,可以帮助学习httpclient,利用httpclient采集网络数据,解决采集时遇到的问题
网上找的,共同分享! HttpClient4.0.3 Tutorial中文翻译!
httpclient4中文教程 httpclient4
《 httpclient-4.5.3中文版》 文档 ,重要的事说三遍:很实用,很实用,很实用!
包含翻译后的API文档:httpclient-4.5.5-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId:org.apache.httpcomponents,artifactId:httpclient,version:4.5.5 使用方法:解压翻译后的API文档,用...
标签:apache、httpcomponents、httpclient、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明...
HttpClient中文文档,快速开发