[简单]commons-httpclient post请求乱码问题记录

        最近工作中需要使用commons-httpclient模拟请求拿到返回值,在浏览器上面直接请求很正常,如图

      

        但是代码返回的结果却是乱码,如下

        

       开始以为是编码问题,而是debug拿到返回值,如下

      

 
      使用各种编码测试

     

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");
	}

    结果如下

   

    全文完
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值