[英]Uncompress GZIP http-response (using jersey client api, java)
Could someone tell me what I need to do in order to uncompress a GZIP content when getting the response from some Http-call. 有人可以告诉我在从某些Http调用获得响应时解压缩GZIP内容需要做些什么。
To make the call I use the Jersey Client API, see code below: 要拨打电话我使用Jersey客户端API,请参阅以下代码:
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);
However the output is GZIP'd and looks like: 但输出是GZIP,看起来像:
{J?J??t??`$?@??????....
It would be good if I could implement the following: 如果我能实现以下内容会很好:
只需将GZIPContentEncodingFilter添加到您的客户端:
client.addFilter(new GZIPContentEncodingFilter(false));
Don't retrieve the response as an entity. 不要将响应检索为实体。 Retrieve it as an input stream and wrap it in a java.util.zip.GZIPInputStream:
将其作为输入流检索并将其包装在java.util.zip.GZIPInputStream中:
GZipInputStream is = new GZipInputStream(response.getEntityInputStream());
Then read the uncompressed bytes yourself and turn it into a String. 然后自己读取未压缩的字节并将其转换为String。
Also, check whether the server is including the HTTP header Content-Encoding: gzip
. 另外,检查服务器是否包含HTTP头
Content-Encoding: gzip
。 If not, try including it in the response. 如果没有,请尝试将其包含在响应中。 Perhaps Jersey is smart enough to do the right thing.
也许泽西岛足够聪明,可以做正确的事情。
In Jersey 2.x (I use 2.26): 在Jersey 2.x(我使用2.26):
WebTarget target = ...
target.register(GZipEncoder.class);
Then getEntity(String.class)
can be used on the response as usual. 然后
getEntity(String.class)
往常一样在响应上使用getEntity(String.class)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.