简体   繁体   English

如何从使用 ThreadSafeClientConnManager 的连接的响应中获取分块页脚?

[英]How do I get chunked footers from a response on a connection that uses ThreadSafeClientConnManager?

I'm using a connection created by ThreadSafeClientConnManager (Apache httpcomponents 4.1.1).我正在使用由 ThreadSafeClientConnManager (Apache httpcomponents 4.1.1) 创建的连接。 The response is chunked (which I expect), as is determined by response.getEntity().isChunked()响应是分块的(我期望的),由 response.getEntity().isChunked() 确定

However, there is no way to get the footers/trailers (which are necessary for our application).但是,没有办法获得页脚/拖车(这是我们的应用程序所必需的)。 Since the response was chunked, I would expect the entity contents to be of type ChunkedInputStream, however the default request director and executor classes used by the client wrap the original response entity (which, from looking at the httpcomponents source would have been a ChunkedInputStream) in a BasicManagedEntity.由于响应是分块的,我希望实体内容的类型为 ChunkedInputStream,但是客户端使用的默认请求主管和执行程序类包装了原始响应实体(从查看 httpcomponents 源来看,它是一个 ChunkedInputStream)在 BasicManagedEntity 中。

In short, I am no longer able to get the footers/trailers off of the response, as BasicManagedEntity does not make the underlying entity available for use.简而言之,我不再能够从响应中获取页脚/拖车,因为 BasicManagedEntity 不会使基础实体可供使用。 Does anyone know how to work around this?有谁知道如何解决这个问题?

For reference, see:供参考,请参阅:

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

One can use an HTTP response interceptor in order to access to the chunked content stream and response footers.可以使用 HTTP 响应拦截器来访问分块内容 stream 和响应页脚。

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();
        }
    }
}

}); });

As described in the answer, this can be done when using the deprecated DefaultHttpClient.如答案中所述,这可以在使用已弃用的 DefaultHttpClient 时完成。 For the newer non-deprecated HttpClients, there is a bug https://issues.apache.org/jira/browse/HTTPCLIENT-1992 which prevents trailers from being accessed in version 4.5.对于较新的未弃用 HttpClients,有一个错误https://issues.apache.org/jira/browse/HTTPCLIENT-1992阻止在版本 4.5 中访问预告片。 This bug has been fixed in 5.0此错误已在 5.0 中修复

So in v4.5, below won't work.所以在 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从java servlet中的chunked响应中发送Http预告片/页脚? - How do I send Http trailers/footers in a chunked response from within a java servlet? 如何在没有传输编码的情况下发送HTTP响应:chunked? - How do I send an HTTP response without Transfer Encoding: chunked? 如何从使用json响应的函数返回值? - How can I return a value from a function that uses json response? Java-如何从多个线程获取或创建连接 - Java - How do I get or create a connection from multiple threads 我如何从这样的 JSON 响应中获取值 - How do I get a value from such a JSON Response 嗨,我如何从 selenium 中的 URL 获取响应令牌 - Hi, How do i get the response token from the URL in selenium 我需要暂停执行,直到我从数据库中获得响应。 我该怎么做? - I need to pause the execution till I get response from my database. How do i do it? 如果我们在指定时间内没有得到响应,如何关闭API连接 - How to close API connection if we do not get response with in specified time 如何在java中获得对http的响应? - How do I get a response to a http in java? 如何在我的Spring MVC Web应用程序中添加.jsp页眉和页脚? - How do I add .jsp headers and footers to my Spring MVC web app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM