[HttpClient]HTTPClient PostMethod 中文乱码问题解决方案(2种)
2017-07-18 09:04
337 查看
HTTPClient PostMethod 中文乱码问题解决方案(2种)
Apache HttpClient ( http://jakarta.apache.org/commons/httpclient/ )
是一个纯 Java 的HTTP 协议的客户端编程工具包, 对 HTTP 协议的支持相当全面,
更多细节也可以参考IBM 网站上的这篇文章 HttpClient入门 (http://www.ibm.com/developerworks/cn/opensource/os-httpclient/ ).
不过在实际使用中, 还是发现按照最基本的方式调用 HttpClient 时, 并不支持 UTF-8 编码。
现在给出2个解决方案:
一、在调用PostMethod方法时设置字符编码:
[java] view
plain copy
print?
PostMethod postMethod = new PostMethod(
"http://127.0.0.1:8080/HttpClientServer/login.do");
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
二、重载PostMethod的getRequestCharSet()方法, 返回我们需要的编码(字符集)名称, 就可以解决 UTF-8 或者其它非默认编码提交 POST 请求时的乱码问题了.
[java] view
plain copy
print?
//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";
}
}
[java] view
plain copy
print?
PostMethod postMethod = new UTF8PostMethod(
"http://127.0.0.1:8080/HttpClientServer/login.do");
三、最后服务器端要配合客户端,设置下编码字符集:
[java] view
plain copy
print?
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//解决中文乱码问题,此步不可少
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String username = (String) request.getParameter("username");
String password = (String) request.getParameter("password");
System.out.println("******************** doPost被执行了 ********************");
System.out.println("您的请求参数为:/tusername:" + username + "/tpassword:"
+ password);
out.print("您的请求参数为:/tusername:" + username + "/tpassword:" + password);
out.flush();
out.close();
}
HTTPClient PostMethod 中文乱码问题解决方案(2种)
Apache HttpClient ( http://jakarta.apache.org/commons/httpclient/ )
是一个纯 Java 的HTTP 协议的客户端编程工具包, 对 HTTP 协议的支持相当全面,
更多细节也可以参考IBM 网站上的这篇文章 HttpClient入门 (http://www.ibm.com/developerworks/cn/opensource/os-httpclient/ ).
不过在实际使用中, 还是发现按照最基本的方式调用 HttpClient 时, 并不支持 UTF-8 编码。
现在给出2个解决方案:
一、在调用PostMethod方法时设置字符编码:
[java] view
plain copy
print?
PostMethod postMethod = new PostMethod(
"http://127.0.0.1:8080/HttpClientServer/login.do");
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
二、重载PostMethod的getRequestCharSet()方法, 返回我们需要的编码(字符集)名称, 就可以解决 UTF-8 或者其它非默认编码提交 POST 请求时的乱码问题了.
[java] view
plain copy
print?
//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";
}
}
[java] view
plain copy
print?
PostMethod postMethod = new UTF8PostMethod(
"http://127.0.0.1:8080/HttpClientServer/login.do");
三、最后服务器端要配合客户端,设置下编码字符集:
[java] view
plain copy
print?
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//解决中文乱码问题,此步不可少
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String username = (String) request.getParameter("username");
String password = (String) request.getParameter("password");
System.out.println("******************** doPost被执行了 ********************");
System.out.println("您的请求参数为:/tusername:" + username + "/tpassword:"
+ password);
out.print("您的请求参数为:/tusername:" + username + "/tpassword:" + password);
out.flush();
out.close();
}
相关文章推荐
- [HttpClient]HTTPClient PostMethod 中文乱码问题解决方案(2种)
- [HttpClient]HTTPClient PostMethod 中文乱码问题解决方案(2种)
- [HttpClient]HTTPClient PostMethod 中文乱码问题解决方案(2种)
- HTTPClient PostMethod 中文乱码问题解决方案(2种)
- HTTPClient PostMethod 中文乱码问题解决方案
- java HTTPClient PostMethod 中文乱码问题解决方法
- java HTTPClient PostMethod 中文乱码问题解决方法
- httpclient中文乱码问题
- HttpClient POST 中文乱码问题
- HttpClient4.5.2版本中POST提交中文参数时乱码问题解决方法
- httpclient 4.3.x 版本 post 中文乱码问题
- httpClient中文乱码问题解决(wap提交)
- httpclient 的 doPost请求中文乱码问题
- 用XMLHTTP Post/Get HTML页面时的中文乱码问题之完全Script解决方案
- HttpClient中文乱码的解决方案
- httpClient中文乱码问题解决(wap提交)收藏
- httpClient中文乱码问题解决(wap提交)
- HttpClient在传参和返回结果的中文乱码问题
- AsyncHttpClient+mysql 中文乱码问题
- HttpClient使用post和get时Json中文乱码解决方案