页面返回response里没设置charset,meta里设置了charset=gbk,获取页面出现中文乱码
,httpclient4.0要怎么设置编码啊?网上资料比较少
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET,"GBK");
以上设置没有用?
代码:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(
"http://www.ningdaren.com/");
HttpResponse response = client.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("登陆前页面返回状态信息----------:");
System.out.println(response.getStatusLine());
Header[] headers = response.getAllHeaders(); // 返回的HTTP头信息
for (int i = 0; i < headers.length; i++) {
System.out.println(headers[i]);
}
//返回页面信息
if(entity!= null){
String responseString = new String(EntityUtils.toString(entity).getBytes("gbk"));
System.out.println(responseString);
}
System.out.println("----------------------------------------");
if (entity != null) {
entity.consumeContent();
}
[b]问题补充:[/b]
具体要怎么设置啊,我试了很多函数都没用。。
httpget.addHeader("Content-Type","application/x-www-form-urlencoded; charset="utf-8")没用啊
把得到的responseString进行下编码就行,如下:
responseString = new String(responseBody.getBytes("ISO-8859-1"),"UTF-8")
这么急用这么新的,3.0都够用了,哈哈,还没研究过4。0
看看这有没帮助
http://diddyrock.iteye.com/blog/338447
http://blog.csdn.net/ostrichmyself/archive/2009/08/29/4496145.aspx
这样试试
[code="java"]httpget.addRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");[/code]
关于乱码的问题, 始终坚持一个原则, 入口和出口的字符编码保持一致。最好都是同一为UTF8