1. 程式人生 > >commons-httpclient post請求亂碼問題記錄(非編碼問題,gzip格式問題)

commons-httpclient post請求亂碼問題記錄(非編碼問題,gzip格式問題)

最近工作中需要使用commons-httpclient模擬請求拿到返回值,在瀏覽器上面直接請求很正常,如圖


但是程式碼返回的結果卻是亂碼,如下:



byte[] bs = new byte[] { 31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -85, 86, 42,
-54, 47, 47, 86, -78, -118, -82, 86, 42, -82, 44, 14, -87, 44,
72, 85, -78, 82, 50, 84, -46, 81, 42, 45, 78, 45, -14, 76, 1,
114, -116, 77, -52, -108, 106, 99
, 117, -108, -118, 74, -14, -100, -13, 83, 32, -46, -75, 0, -27, 121, -125, -98, 55, 0, 0, 0 }; String r2 = new String(bs, "utf-8"); String r3 = new String(bs, "gbk"); String r4 = new String(bs, "ISO8859-1"); String r5 = new String(bs); String r6 = new String(r3.getBytes(), "utf-8"); System.out.println(r2); System.out
.println(r3); System.out.println(r4); System.out.println(r5); System.out.println(r6);

確定不是編碼問題後,反編譯jar,debug看程式碼,試了很多次都沒發現問題,

偶然發現瀏覽器返回的值是用gzip編碼的,如下

而是試著看下模擬請求是否也是一樣,結果如下

  確定原因後,只要解壓一下就可以了

if(rtnStatus==200){
          Header[] header=post.getResponseHeaders("Content-Encoding");
          if(header!=null&&header.length>0){
            if("gzip".equals(header[0].getValue())){
              rtnJson = new String(uncompress(post.getResponseBody()));
            }else{
              rtnJson = new String(post.getResponseBody(), "UTF-8");
            }
          }else{
            rtnJson = new String(post.getResponseBody(), "UTF-8");
          }
        }


public static String uncompress(byte[] bytes) throws IOException {
if (bytes == null || bytes.length == 0) {
return "";
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
return out.toString("utf-8");
}

結果如下


全文完