有人能告诉我,当从某个Http-调用获得响应时,我需要做什么才能解压缩GZIP内容。
要进行调用,我使用泽西客户机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);但是,输出是GZIP的,如下所示:
{J?J??t??`$?@??????....如果我能实现以下几点,那就太好了:
中的内容
发布于 2011-09-27 19:30:55
只需将GZIPContentEncodingFilter添加到客户端:
client.addFilter(new GZIPContentEncodingFilter(false));发布于 2011-09-25 16:27:37
不要以实体的形式检索响应。将其检索为输入流,并将其包装在java.util.zip.GZIPInputStream中:
GZipInputStream is = new GZipInputStream(response.getEntityInputStream());然后自己读取未压缩的字节并将其转换为字符串。
另外,检查服务器是否包含header Content-Encoding: gzip。如果没有,请尝试将其包含在响应中。也许泽西有足够的智慧去做正确的事情。
发布于 2017-12-06 04:48:53
在泽西岛2.x (我使用2.26):
WebTarget target = ...
target.register(GZipEncoder.class);然后,可以像往常一样在响应上使用getEntity(String.class)。
https://stackoverflow.com/questions/7546783
复制相似问题