`
810364804
  • 浏览: 786845 次
文章分类
社区版块
存档分类
最新评论

使用httpclient模拟登陆时乱码问题

 
阅读更多

最近在弄一个模拟登陆的程序,拿某网站调试程序,但response返回的总是乱码。

Google了半天,用了各种方法依旧乱码。

使用的方法:

if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
entity = response.getEntity();
if (entity != null) {
//第一种方式
System.out.println(EntityUtils.toString(entity,"GBK"));


//第二种方式

String outstr = new String(EntityUtils.toString(entity).getBytes("ISO-8859-1"),"GBK");
System.out.println(outstr);

//第三种方式

String responseString = new String(EntityUtils.toString(entity));
responseString=new String(responseString.getBytes("ISO-8859-1"),"GBK");
System.out.println(responseString);

}
}

这三种方式都不起作用,一直乱码。

然后又找到另一种方式,在httpclient之前设置编码

client.getParams().setParameter("http.protocol.content-charset", "UTF-8");
this.response = client.execute(hp);

依旧无效。

就在即将放弃的时候,突然灵光乍现。

网站是GET方式,而我采用的事post方式。

改成GET方式后,正常返回了。

O(∩_∩)O~

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics