Java 如何从使用ThreadSafeClientConnManager的连接上的响应中获取大块页脚?
我正在使用由ThreadSafeClientConnManager(Apache httpcomponents 4.1.1)创建的连接。响应被分块(我希望是这样),这是由response.getEntity().isChunked()决定的 但是,无法获取页脚/拖车(这是我们的应用程序所必需的)。由于响应是分块的,我希望实体内容的类型为ChunkedInputStream,但是客户端使用的默认请求控制器和执行器类将原始响应实体(从httpcomponents源来看,它应该是一个ChunkedInputStream)包装在BasicManager身份中 简言之,我再也无法从响应中删除页脚/拖尾,因为BasicManagedEntity不能使基础实体可用。有人知道如何解决这个问题吗 有关参考,请参阅:Java 如何从使用ThreadSafeClientConnManager的连接上的响应中获取大块页脚?,java,footer,chunked-encoding,apache-httpcomponents,Java,Footer,Chunked Encoding,Apache Httpcomponents,我正在使用由ThreadSafeClientConnManager(Apache httpcomponents 4.1.1)创建的连接。响应被分块(我希望是这样),这是由response.getEntity().isChunked()决定的 但是,无法获取页脚/拖车(这是我们的应用程序所必需的)。由于响应是分块的,我希望实体内容的类型为ChunkedInputStream,但是客户端使用的默认请求控制器和执行器类将原始响应实体(从httpcomponents源来看,它应该是一个ChunkedIn
- org.apache.http.impl.client.DefaultRequestDirector.java,第523-525行
- org.apache.http.impl.entity.EntityDeserializer.java,第93-96行
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();
}
}
}
})) 如回答中所述,这可以在使用不推荐使用的DefaultHttpClient时完成。对于较新的未弃用的HttpClient,有一个bug阻止在版本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();
旁注:我已经通过为HttpClient、HttpResponse和HttpRequestExecutor实现各种子类和包装器解决了这个问题,但我仍然在寻找更好的解决方案!