1

我正在尝试通过代理发送 GET 并且某些站点具有标头:Content-Encoding: none,这会导致 Apache 引发异常。我想知道这是否是预期的行为,以及我是否应该将其视为错误:

Caused by: org.apache.http.HttpException: Unsupported Content-Coding: none
at org.apache.http.client.protocol.ResponseContentEncoding.process(ResponseContentEncoding.java:98)
at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:139)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:199)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
... 10 more

我的代码:

public CloseableHttpResponse getViaProxy(String url, String ip, int port, String username,
                                         String password) {
    CloseableHttpClient httpClient;
    if (username == null) {
        httpClient = HttpClients.custom()
                .setSSLSocketFactory(getCustomSslConnectionSocketFactory())
                .build();
    } else {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(ip, port),
                new UsernamePasswordCredentials(username, password));
        httpClient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider)
                .setSSLSocketFactory(getCustomSslConnectionSocketFactory())
                .build();
    }

    RequestConfig config = RequestConfig.custom()
            .setProxy(new HttpHost(ip, port))
            .build();
    HttpGet httpGet = new HttpGet(url);
    httpGet.setConfig(config);

    return httpClient.execute(httpGet);
}

我指的错误来自这里。似乎此方法仅支持带有 Content-Encoding 的标头:gzip、deflate 或 identity。

http://hc.apache.org/httpcomponents-client-ga/httpclient/xref/org/apache/http/client/protocol/ResponseContentEncoding.html

4

3 回答 3

4

HTTP 协议规范将gzipcompress和定义deflateidentity有效的内容编码方案。应该使用它identity来表示不需要内容转换。

于 2013-12-09T11:20:51.513 回答
1

如果只想处理 Content-Encoding 为 none 的请求,可以使用 customHttpClent 的方法,如下所示:

CloseableHttpClient httpClient = HttpClients.custom().disableContentCompression().build();

此代码禁用两个拦截器RequestAcceptEncodingResponseContentEncoding.

于 2013-12-25T12:55:42.357 回答
1

有可能使用拦截器在客户端解决该问题:http: //adamscheller.com/java/httpexception-unsupported-content-coding-none-solution/

于 2016-02-09T21:20:35.317 回答