我需要发送一个HTTPPost身体gzipped,服务器也接受非gzip的数据也喜欢它gzipped,所以我试图转换一些现有的工作代码使用gzip 数据当前设定问题使用Apache客户端4.1.1
httpMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
伊夫试图sublclassing HttpEntityWrapper
static class GzipWrapper extends HttpEntityWrapper
{
public GzipWrapper(HttpEntity wrapped)
{
super(wrapped);
}
public void writeTo(OutputStream outstream)
throws IOException
{
GZIPOutputStream gzip = new GZIPOutputStream(outstream);
super.writeTo(gzip);
}
}
和改变为
httpMethod.setEntity(new GzipWrapper(
new UrlEncodedFormEntity(nameValuePairs)));
,并添加
if (!httpMethod.containsHeader("Accept-Encoding"))
{
httpMethod.addHeader("Accept-Encoding", "gzip");
}
,但现在我的要求只是超时我想一定是坏了我的GZIpWrapper但林不知道。
在另一张纸条上,我看着http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientGZipContentCompression.java。例。除了我不喜欢拦截器这一事实,因为它很难遵循程序流程,所以它对我来说没有意义,因为请求标头设置为告诉服务器接受gzip数据,但实际上并没有对gzip编码任何数据进行编码,它只解压缩响应。
Thx Oleg,(好吧,我现在得到它,当你得到请求的Accept-Encoding头时,它告诉服务器它可以从服务器响应中接受什么样的补偿) – 2011-05-13 05:53:07