java - 为什么 apache java http 库不处理内容编码为 : none? 的站点

标签 java apache-httpclient-4.x content-encoding

我正在尝试通过代理发送 GET,而某些站点具有 header :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);
}

我指的错误来自这里。似乎此方法仅支持具有内容编码的 header :gzip、deflate 或 identity。

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

最佳答案

HTTP 协议(protocol)规范将gzipcompressdeflateidentity 定义为有效的内容编码方案。应该使用 identity 来表示不需要进行内容转换。

关于java - 为什么 apache java http 库不处理内容编码为 : none? 的站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20429722/

相关文章:

java - 关于 org.apache.http.client.HttpClient 的好教程

internet-explorer - 我们如何验证 gzip/deflate 压缩在 IE 浏览器中是否有效?

java - http 请求中内容编码 header 中多个值的排序

java - 以子类作为参数的重载方法,但将方法称为父类(super class)

java - 当我启动使用 DWR3.0 RC2 和 Spring 的项目时出现错误

java - 在 Java 中解析 HTML 字符串中的数字

java - 使用 NetBeans 在 MS Access 数据库中未发生插入删除更新

java - 相当于 HttpClient 4.x 中的 MultiThreadedHttpConnectionManager.shutdownAll()

java - 如何在 Rest Assured 中为失败的 REST 调用实现重试逻辑

java - 如何区分 HTTPURLConnection 中的请求和响应?