HttpClient乱码问题 - wangtao20090608的专栏 - 博客频道 - CSDN.NET

HttpClient乱码问题

2010-09-07 23:00 109人阅读 评论(0) 收藏 举报

服务端Servlet代码:

[c-sharp] view plaincopyprint?
  1. package com.httpclient.test;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. public class HttpClientServlet extends HttpServlet {
  9. @Override
  10. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  11. throws ServletException, IOException {
  12. this.doGet(req, resp);
  13. }
  14. @Override
  15. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  16. throws ServletException, IOException {
  17. req.setCharacterEncoding("UTF-8");
  18. resp.setCharacterEncoding("GBK");
  19. String username = req.getParameter("username");
  20. String password = req.getParameter("password");
  21. System.out.println(username);
  22. PrintWriter writer = resp.getWriter();
  23. writer.print(username + " = " + password);
  24. writer.close();
  25. }
  26. }

package com.httpclient.test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HttpClientServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("GBK"); String username = req.getParameter("username"); String password = req.getParameter("password"); System.out.println(username); PrintWriter writer = resp.getWriter(); writer.print(username + " = " + password); writer.close(); } }

HttpClient客户端代码:

[c-sharp] view plaincopyprint?
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.chinapay.client;
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import org.apache.commons.httpclient.HttpClient;
  11. import org.apache.commons.httpclient.HttpStatus;
  12. import org.apache.commons.httpclient.NameValuePair;
  13. import org.apache.commons.httpclient.methods.PostMethod;
  14. import org.apache.commons.httpclient.params.HttpMethodParams;
  15. public class HttpClientPost {
  16. public static void main(String[] args) throws IOException{
  17. HttpClient client = new HttpClient();
  18. client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
  19. String url = "http://localhost:8080/httpclient/HttpClientServlet";
  20. PostMethod postMethod = new PostMethod(url);
  21. NameValuePair[] data = {
  22. new NameValuePair("username", "你好")
  23. ,new NameValuePair("password", "wangtao") };
  24. postMethod.setRequestBody(data);
  25. int resultCode = client.executeMethod(postMethod);
  26. if(resultCode != HttpStatus.SC_OK){
  27. System.err.println("Method failed:" + postMethod.getStatusLine());
  28. }
  29. InputStream responseBody = postMethod.getResponseBodyAsStream();
  30. BufferedReader reader = new BufferedReader(new InputStreamReader(responseBody,"GBK"));
  31. String tempBf = null;
  32. while((tempBf=reader.readLine()) != null){
  33. System.out.println(tempBf);
  34. }
  35. }
  36. }

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.chinapay.client; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; public class HttpClientPost { public static void main(String[] args) throws IOException{ HttpClient client = new HttpClient(); client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); String url = "http://localhost:8080/httpclient/HttpClientServlet"; PostMethod postMethod = new PostMethod(url); NameValuePair[] data = { new NameValuePair("username", "你好") ,new NameValuePair("password", "wangtao") }; postMethod.setRequestBody(data); int resultCode = client.executeMethod(postMethod); if(resultCode != HttpStatus.SC_OK){ System.err.println("Method failed:" + postMethod.getStatusLine()); } InputStream responseBody = postMethod.getResponseBodyAsStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(responseBody,"GBK")); String tempBf = null; while((tempBf=reader.readLine()) != null){ System.out.println(tempBf); } } }

client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");和req.setCharacterEncoding("UTF-8");编码格式相同,BufferedReader reader = new BufferedReader(new InputStreamReader(responseBody,"GBK"));和resp.setCharacterEncoding("GBK");的编码格式相同。这样就可以解决乱码问题了。

HttpClient乱码问题相关推荐

  1. android httpclient 乱码,【问题解决】HttpClient解析服务器返回的response出现乱码

    问题场景 最近在用httpClient做网络爬虫的时候,遇到了一个不大不小的问题,当使用HttpGet向指定网址发送请求后,接收到的Response无法正常解析,出现 口口??这样的乱码,编码也考虑到 ...

  2. httpclient工具类,post请求发送json字符串参数,中文乱码处理

    在使用httpclient发送post请求的时候,接收端中文乱码问题解决. 正文: 我们都知道,一般情况下使用post请求是不会出现中文乱码的.可是在使用httpclient发送post请求报文含中文 ...

  3. 关于HttpClient上传中文乱码的解决办法

    使用过HttpClient的人都知道可以通过addTextBody方法来添加要上传的文本信息,但是,如果要上传中文的话,或还有中文名称的文件会出现乱码的问题,解决办法其实很简单: 第一步:设置Mult ...

  4. 左右HttpClient上传的方法来解决中国的乱码

    二手HttpClient人们都知道通过addTextBody方法来加入要上传的文本信息,可是,假设要上传中文的话.或还有中文名称的文件会出现乱码的问题,解决的方法事实上非常easy: 第一步:设置Mu ...

  5. HttpClient测试类请求端和服务端即可能出现乱码的解决

    junit HttpClient 请求端 代码: package com.taotao.httpclient;import java.util.ArrayList; import java.util. ...

  6. HttpClient_002_中文乱码、HttpClient中文乱码透析、总结

    中文乱码原理代码: String s = "中文";byte[] bs2 = s.getBytes("utf-8");//将s拆成:utf-8个体,注:utf- ...

  7. Java的HttpClient类以POST方式提交数据,目标端收到后中文乱码

     h ttpClient HttpMethod NameValuePair setRequestBody 今天开发时,遇到利用Java中HttpClient类以POST方式提交数据,目标收到后中文 ...

  8. [HttpClient]HTTPClient PostMethod 中文乱码问题解决方案(2种)

     HTTPClient PostMethod 中文乱码问题解决方案(2种) Apache HttpClient ( http://jakarta.apache.org/commons/httpcl ...

  9. Httpclient gzip 乱码问题解决

    ■出现问题的原因推测 被反爬了,缺少了cookie,你请求出来的信息就是运行一段js, 生成cookie,看到args1了么,这个是密钥,下面的也不是编码的,就是js混淆的问题 防爬网站需要携带一些基 ...

最新文章

  1. 手机php转换txt,PHP 实现的将图片转换为TXT
  2. Label的作用是什么?是怎么用的?
  3. C语言数组+冒泡排序
  4. STM32F0使用LL库实现PWM输出
  5. 蚂蚁集团技术专家山丘:性能优化常见压测模型及优缺点
  6. MultipartFile与File之间的相互转换
  7. Objective-C入门教程(摘录)
  8. 拓端tecdat|Matlab正态分布、历史模拟法、加权移动平均线 EWMA估计风险价值VaR和回测Backtest标准普尔指数 SP500时间序列
  9. python 三维矩阵乘以二维矩阵_python 二维矩阵转三维矩阵示例
  10. 【ruby】ruby图像处理模块“mini_magick”
  11. 删除linkinfo.dll
  12. 在家登录学校图书馆知网
  13. 怎么将图片无损放大?手把手教你们这3种无损放大的方法
  14. 图片上传的两种方式(前端和后端)
  15. 详解数据仓库建设体系
  16. 修改服务器tcp会话数,windows2012服务器TCP连接数
  17. Spring整合日志框架Log4j2
  18. XTransfer又双叒叕拿奖,XTransfer外贸收款这么靠谱吗?
  19. 写给我们终将逝去的青春
  20. 系统修复软件测试工资,技术员们有福了,关于怎么用MHDD修复硬盘坏道,现在免费给大家了!!!...

热门文章

  1. [分享]毕业了【其实不想毕业】
  2. 主流浏览器新版本将不再支持TLS 1.0/1.1
  3. 手把手教你学Vue-3(路由)
  4. 双11大幕拉开,菜鸟智能机器人也将测试运行
  5. 开篇有益-解析微软微服务架构eShopOnContainers(一)
  6. linux的文件系统及节点表
  7. 关于autorelease pool一个较好的理解
  8. Android应用资源---布局资源类型(Layout)
  9. 测试基础 – 软件测试计划
  10. 认识 Web.config