2

我正在使用由 ThreadSafeClientConnManager (Apache httpcomponents 4.1.1) 创建的连接。响应是分块的(我期望的),由 response.getEntity().isChunked() 确定

但是,没有办法获得页脚/拖车(这是我们的应用程序所必需的)。由于响应被分块,我希望实体内容的类型为 ChunkedInputStream,但是客户端使用的默认请求导向器和执行器类包装了原始响应实体(从 httpcomponents 源来看,它是一个 ChunkedInputStream)在 BasicManagedEntity 中。

简而言之,我不再能够从响应中获取页脚/拖车,因为 BasicManagedEntity 不会使基础实体可供使用。有谁知道如何解决这个问题?

供参考,请参阅:

  • org.apache.http.impl.client.DefaultRequestDirector.java,第 523-525 行
  • org.apache.http.impl.entity.EntityDeserializer.java,第 93-96 行
4

2 回答 2

3

可以使用 HTTP 响应拦截器来访问分块的内容流和响应页脚。

httpclient.addResponseInterceptor(new HttpResponseInterceptor() {

public void process(
        final HttpResponse response,
        final HttpContext context) throws HttpException, IOException {
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        if (instream instanceof ChunkedInputStream) {
            Header[] footers = ((ChunkedInputStream) instream).getFooters();
        }
    }
}

});

于 2011-07-08T11:40:49.203 回答
0

如答案中所述,这可以在使用已弃用的 DefaultHttpClient 时完成。对于较新的非弃用 HttpClients,有一个错误https://issues.apache.org/jira/browse/HTTPCLIENT-1992阻止在 4.5 版中访问预告片。此错误已在 5.0 中修复

所以在 v4.5 中,下面将不起作用。

 CloseableHttpClient httpclient = HttpClients.custom().addInterceptorFirst(
            (org.apache.http.HttpResponse response, HttpContext context) -> {
                InputStream instream = response.getEntity().getContent();
                if (instream instanceof ChunkedInputStream) {
                    //Code will never run for v4.5
                }
            }
    ).build();
于 2021-02-09T06:41:38.840 回答