日期:2014-05-19  浏览次数:20872 次

httpclient输出响应内容为乱码
DefaultHttpClient client = null;
client = new DefaultHttpClient();

client.getParams()
.setParameter(CoreProtocolPNames.USER_AGENT,

"Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 

Firefox/11.0");
client.getParams().setParameter(
CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");

client.setHttpRequestRetryHandler(requestRetryHandler);



HttpPost hp = new HttpPost("http://www.intertops.eu/logon.htm?lan=en");


  hp.addHeader。。。。。//hp增加请求头

List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add。。。。 //list添加参数

HttpResponse response;
UrlEncodedFormEntity uefEntity;
try {

hp.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));


} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
System.out.println("UnsupportedEncodingException");
e1.printStackTrace();
}
try {


response = client.execute(hp);

System.out.println(EntityUtils.toString(response.getEntity()));

} catch (Exception e) {
e.printStackTrace();
response = null;
}




// 使用ResponseHandler接口处理响应,HttpClient使用ResponseHandler会自动管理连接的释放,解决了对连接的释放管理
private static ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
// 自定义响应处??
public String handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {

HttpEntity entity = response.getEntity();
if (entity != null) {
String charset = EntityUtils.getContentCharSet(entity) == null ? "UTF-8"
: EntityUtils.getContentCharSet(entity);
return new String(EntityUtils.toByteArray(entity), charset);
} else {
return null;
}
}
};


输出内容是一堆乱码,求答案
?

------解决方案--------------------
可以试试这样转码……
String ?="";

String s=new String(super.getRequest().getParameter(?).getBytes("ISO-8859-1;charset=utf-8"));