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、または ID を持つヘッダーのみをサポートしているようです。

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

4

3 に答える 3

1

Content-Encoding が none のリクエストのみを処理したい場合は、 custom次のように HttpClent のメソッドを使用できます。

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

このコードは、2 つのインターセプタRequestAcceptEncodingと を無効にしResponseContentEncodingます。

于 2013-12-25T12:55:42.357 に答える