- 在使用HttpClient过程中发现有时候发出请求后,返回的结果是乱码,使用下面的方法可以有效的解决这个问题:
-
- String content = "";
- try{
- HttpPost post = new HttpPost(url);
- HttpResponse response = httpClient.execute(post);
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- // 使用EntityUtils的toString方法,传递默认编码,在EntityUtils中的默认编码是ISO-8859-1
- //toString 重载了几个不同的方法可供参考
- content = EntityUtils.toString(entity, charset);
- }
- }
- catch(Exception e){
- e.printStackTrace();
- }
解决 httpclient 返回乱码
最新推荐文章于 2024-07-09 14:42:08 发布