解压缩GZIP http响应(使用jersey客户端api,java) [英] Uncompress GZIP http-response (using jersey client api, java)

查看:577
本文介绍了解压缩GZIP http响应(使用jersey客户端api,java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我从一些Http调用中获取响应时,要解压缩GZIP内容需要做什么.

Could someone tell me what I need to do in order to uncompress a GZIP content when getting the response from some Http-call.

要使用泽西客户端API进行呼叫,请参见以下代码:

To make the call I use the Jersey Client API, see code below:

String baseURI = "http://api.stackoverflow.com/1.1/answers/7539863?body=true&comments=false";
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource wr = client.resource(baseURI); 
ClientResponse response = null;
response = wr.get(ClientResponse.class);
String response_data = response.getEntity(String.class);

System.out.println(response_data);

但是输出是GZIP,看起来像:

However the output is GZIP’d and looks like:

{J?J??t??`$?@??????....

如果我可以实现以下功能,那就太好了:

It would be good if I could implement the following:

  • 能够检测内容是否为GZIP;
  • 如果不是,则按正常方式在String中进行处理;如果是,则解压缩并获取String中的内容

推荐答案

只需添加

Simply add GZIPContentEncodingFilter to your client:

client.addFilter(new GZIPContentEncodingFilter(false));

这篇关于解压缩GZIP http响应(使用jersey客户端api,java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆