[英]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:供参考,请参阅:
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.