其他分享
首页 > 其他分享> > HttpClient请求中HttpResponse返回乱码问题解决。

HttpClient请求中HttpResponse返回乱码问题解决。

作者:互联网

问题:EntityUtils.toString(response.getEntity()) 打印出乱码
解决:返回值数据较大时会以压缩格式返回,判断请求头中包含“gzip”,需进行特殊处理,代码如下。

public static String reqGet(String url,Map<String,String> headers){
    HttpClient client=HttpConnectionManager.getHttpClient();
    HttpParams params = new BasicHttpParams();
    String respBody="";
      try {
         HttpGet get =new HttpGet(url);
         Set<String> headersSet = headers.keySet();
         for(String key:headersSet){
            get.setHeader(key, headers.get(key));
         }
         get.setParams(params);
         HttpClientParams.setCookiePolicy(client.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);

         HttpResponse resp = client.execute(get);
          if(resp.getStatusLine().getStatusCode() == 200){
             respBody = getStringFromResponse(resp);
          }
      }catch (Exception e) {
         e.printStackTrace();
      }
      return respBody;
}
private static String getStringFromResponse(HttpResponse response) {
   if (response == null) {
      return null;
   }
   String responseText = "";
   try {
      HttpEntity entity = response.getEntity();
      InputStream in = entity.getContent();;
      Header[] headers = response.getHeaders("Content-Encoding");
      for (Header h : headers) {
         if (h.getValue().indexOf("gzip") > -1) {
            //For GZip response
            GZIPInputStream gzin = new GZIPInputStream(in);
            InputStreamReader isr = new InputStreamReader(gzin, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(isr);
            String lineTXT = null;
            while ((lineTXT = bufferedReader.readLine()) != null) {
               responseText += lineTXT.toString();
            }
            isr.close();
            break;
         }
      }
      if(StringUtils.isEmpty(responseText)){
         responseText = EntityUtils.toString(entity);
      }
   } catch (IOException exception) {
      exception.printStackTrace();
   }
   return responseText;
}

标签:responseText,String,get,乱码,headers,new,HttpResponse,response,HttpClient
来源: https://blog.csdn.net/yh_852974543/article/details/121275962