HTTPCLIENT 基本应用 解决编码乱码问题

先看代码,方便以后复制,就不作为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));这句话不设置就可能导致乱码。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值