我需要發送一個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