HttpClient乱码问题
HttpClient乱码问题 - wangtao20090608的专栏 - 博客频道 - CSDN.NET
HttpClient乱码问题
2010-09-07 23:00 109人阅读 评论(0) 收藏 举报服务端Servlet代码:
[c-sharp] view plaincopyprint?
- 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();
- }
- }
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?
- /*
- * 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);
- }
- }
- }
/* * 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乱码问题相关推荐
- android httpclient 乱码,【问题解决】HttpClient解析服务器返回的response出现乱码
问题场景 最近在用httpClient做网络爬虫的时候,遇到了一个不大不小的问题,当使用HttpGet向指定网址发送请求后,接收到的Response无法正常解析,出现 口口??这样的乱码,编码也考虑到 ...
- httpclient工具类,post请求发送json字符串参数,中文乱码处理
在使用httpclient发送post请求的时候,接收端中文乱码问题解决. 正文: 我们都知道,一般情况下使用post请求是不会出现中文乱码的.可是在使用httpclient发送post请求报文含中文 ...
- 关于HttpClient上传中文乱码的解决办法
使用过HttpClient的人都知道可以通过addTextBody方法来添加要上传的文本信息,但是,如果要上传中文的话,或还有中文名称的文件会出现乱码的问题,解决办法其实很简单: 第一步:设置Mult ...
- 左右HttpClient上传的方法来解决中国的乱码
二手HttpClient人们都知道通过addTextBody方法来加入要上传的文本信息,可是,假设要上传中文的话.或还有中文名称的文件会出现乱码的问题,解决的方法事实上非常easy: 第一步:设置Mu ...
- HttpClient测试类请求端和服务端即可能出现乱码的解决
junit HttpClient 请求端 代码: package com.taotao.httpclient;import java.util.ArrayList; import java.util. ...
- HttpClient_002_中文乱码、HttpClient中文乱码透析、总结
中文乱码原理代码: String s = "中文";byte[] bs2 = s.getBytes("utf-8");//将s拆成:utf-8个体,注:utf- ...
- Java的HttpClient类以POST方式提交数据,目标端收到后中文乱码
h ttpClient HttpMethod NameValuePair setRequestBody 今天开发时,遇到利用Java中HttpClient类以POST方式提交数据,目标收到后中文 ...
- [HttpClient]HTTPClient PostMethod 中文乱码问题解决方案(2种)
HTTPClient PostMethod 中文乱码问题解决方案(2种) Apache HttpClient ( http://jakarta.apache.org/commons/httpcl ...
- Httpclient gzip 乱码问题解决
■出现问题的原因推测 被反爬了,缺少了cookie,你请求出来的信息就是运行一段js, 生成cookie,看到args1了么,这个是密钥,下面的也不是编码的,就是js混淆的问题 防爬网站需要携带一些基 ...
最新文章
- 手机php转换txt,PHP 实现的将图片转换为TXT
- Label的作用是什么?是怎么用的?
- C语言数组+冒泡排序
- STM32F0使用LL库实现PWM输出
- 蚂蚁集团技术专家山丘:性能优化常见压测模型及优缺点
- MultipartFile与File之间的相互转换
- Objective-C入门教程(摘录)
- 拓端tecdat|Matlab正态分布、历史模拟法、加权移动平均线 EWMA估计风险价值VaR和回测Backtest标准普尔指数 SP500时间序列
- python 三维矩阵乘以二维矩阵_python 二维矩阵转三维矩阵示例
- 【ruby】ruby图像处理模块“mini_magick”
- 删除linkinfo.dll
- 在家登录学校图书馆知网
- 怎么将图片无损放大?手把手教你们这3种无损放大的方法
- 图片上传的两种方式(前端和后端)
- 详解数据仓库建设体系
- 修改服务器tcp会话数,windows2012服务器TCP连接数
- Spring整合日志框架Log4j2
- XTransfer又双叒叕拿奖,XTransfer外贸收款这么靠谱吗?
- 写给我们终将逝去的青春
- 系统修复软件测试工资,技术员们有福了,关于怎么用MHDD修复硬盘坏道,现在免费给大家了!!!...