先看代码,方便以后复制,就不作为code格式了。
package com.amazon.commons;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class HttpClientHelper {
public static String RESPONSE_TEXT="responseText";
public static String CHARSET="charset";
public static void main(String[] args) {
System.out.println(get("http://www.baidu.com/").get(HttpClientHelper.RESPONSE_TEXT));
}
public static Map<String, String> get(String url) {
Map<String, String> result=new HashMap<String, String>();
// Create an instance of HttpClient.
HttpClient client = new HttpClient();
// Create a method instance.
GetMethod method = new GetMethod(url);
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);
String charset=method.getResponseCharSet();
System.out.println(charset);
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
// Read the response body.
/**
* Note: This will cause the entire response body to be buffered in memory.
* A malicious server may easily exhaust all the VM memory.
* It is strongly recommended, to use getResponseAsStream if the content
* length of the response is unknown or resonably large.
*/
byte[] responseBody = method.getResponseBody();
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary
// data
result.put(HttpClientHelper.RESPONSE_TEXT, new String(responseBody,charset));
result.put(HttpClientHelper.CHARSET, charset);
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
return result;
}
}
}
查看控制台我们可以看到,百度的页面编码设置是gb2312.
核心代码:
int statusCode = client.executeMethod(method);
String charset=method.getResponseCharSet();
System.out.println(charset);
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
result.put(HttpClientHelper.RESPONSE_TEXT, new String(responseBody,charset));
result.put(HttpClientHelper.CHARSET, charset);
这里我之所以把charset作为一个返回参数是有原因的,因为在action或者servlet调用完这个之后如果只返回一个responseText,那么response在打印输出的时候是不知道这段text的编码的,
action代码:
try {
HttpServletResponse response= ServletActionContext.getResponse();
response.setCharacterEncoding(result.get(HttpClientHelper.CHARSET));
PrintWriter writer=response.getWriter();
writer.write(result.get(HttpClientHelper.RESPONSE_TEXT));
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setCharacterEncoding(result.get(HttpClientHelper.CHARSET));这句话不设置就可能导致乱码。
分享到:
相关推荐
下载这个工具类,然后直接调用createHtmlPage方法就可以了。最关键一点是,网上的方法没有一个解决了乱码问题,费了我不少功夫。
服务器端自己写,本示例为客户端代码,可以解决httpClient4乱码问题,是替代修改源码的最佳写法。
很全面的代码介绍,很完整的乱码解决说明,让你一目了然,值得研究
解决HttpClient中文乱码问题jar文件
HttpClient4.2.5实现文件上传,无中文文件名乱码问题。亲测无问题。有问题可吐槽!
我使用的是httpClient 进行内部转发 我们在A的服务器上,将前台的文件流,通过httpClient传输到B的服务器上(B的服务器通过控制层接受A传输的文件流,让后保存在B的服务器上。返回一个json结果)
学习C#必经之路,入门级知识总结,笔记本知识概况,走一步再走一步
在开发中遇到乱码问题 上传文件并传其它参数时 按网上多种方法尝试设置UTF-8编码总是不好用,只好修改源码统一编码为utf-8 后打包,解决问题 此包没有经过其它编码环境下测试,不过应该不会有其它影响
HttpClient 基本用法,HttpClient 基本用法
HTTPClient组件的应用
httpclient3 httpclient4 上传文件 乱码问题
使用httpclient 3.0解析html,实际使用当中遇到的字符集编码乱码问题,主要用于网页的信息采集,此外还列出一部分截取字符串的方法,带源码 httpclient3.0 自己下载jar包
HttpClient之Https应用实例~ 包含: HttpClient 使用代理访问Https HttpClient 信任所有主机-对于任何证书都不做检查 HttpClient 允许所有证书的校验(包括过期证书)
* 解决中文乱码问题 * @param httpUrl * @return */ public static String downloadPage(String httpUrl) { StringBuffer pageBuffer = new StringBuffer(); URL pageUrl = null; try { pageUrl =...
基于httpClient的文件编码导入系统
这是httpclient应用所有jar,用户抓取请求内容,本人平时用的就是这个,欢迎下载!
应用HttpClient时添加的代码。应用HttpClient时添加的代码。应用HttpClient时添加的代码。应用HttpClient时添加的代码。应用HttpClient时添加的代码。应用HttpClient时添加的代码。应用HttpClient时添加的代码。
HTTP 协议可能是现在 Internet 上...HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。现在HttpClient最新版本为 HttpClient 4.2 (GA)
HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java ...HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。
httpclient