如何使用apache java HttpClient 4.5从连接请求中检索响应头?
我使用http代理通过CloseableHttpClient连接https网站。首先发送连接请求,因为它必须进行隧道连接。将有响应头发送,我需要检索它们。结果将存储在变量中,稍后使用 在HttpClient 4.2上,我可以使用HttpResponseInterceptor实现它:如何使用apache java HttpClient 4.5从连接请求中检索响应头?,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,我使用http代理通过CloseableHttpClient连接https网站。首先发送连接请求,因为它必须进行隧道连接。将有响应头发送,我需要检索它们。结果将存储在变量中,稍后使用 在HttpClient 4.2上,我可以使用HttpResponseInterceptor实现它: final DefaultHttpClient-httpClient=new DefaultHttpClient(); //配置代理 httpclient.addResponseInterceptor(新的HttpR
final DefaultHttpClient-httpClient=new DefaultHttpClient();
//配置代理
httpclient.addResponseInterceptor(新的HttpResponseInterceptor(){
公共无效进程(HttpResponse HttpResponse,HttpContext HttpContext)抛出HttpException,IOException{
final Header[]headers=httpResponse.getAllHeaders();
//存储头
}
});
但当我使用HttpClient 4.5时,我的响应拦截器从未在连接请求时被调用。它在GET或POST请求时直接跳转:
final CloseableHttpClient-httpClient=HttpClients.custom()
.setProxy(新的HttpHost(proxyHost,proxyPort,“http”))
.addInterceptorFirst(新的HttpResponseInterceptor(){
公共无效进程(HttpResponse HttpResponse,HttpContext HttpContext)抛出HttpException,IOException{
final Header[]headers=httpResponse.getAllHeaders();
//存储头
}
}).build();
方法setInterceptorLast()
和setHttpProcessor()
的结果相同。
提前感谢:)您需要一个自定义的
HttpClientBuilder
类,以便能够向用于执行CONNECT
方法的HTTP协议处理器添加自定义协议侦听器
HttpClient 4.5
这并不漂亮,但可以完成工作:
HttpClientBuilder builder = new HttpClientBuilder() {
@Override
protected ClientExecChain createMainExec(
HttpRequestExecutor requestExec,
HttpClientConnectionManager connManager,
ConnectionReuseStrategy reuseStrategy,
ConnectionKeepAliveStrategy keepAliveStrategy,
HttpProcessor proxyHttpProcessor,
AuthenticationStrategy targetAuthStrategy,
AuthenticationStrategy proxyAuthStrategy,
UserTokenHandler userTokenHandler) {
final ImmutableHttpProcessor myProxyHttpProcessor = new ImmutableHttpProcessor(
Arrays.asList(new RequestTargetHost()),
Arrays.asList(new HttpResponseInterceptor() {
@Override
public void process(
HttpResponse response,
HttpContext context) throws HttpException, IOException {
}
})
);
return super.createMainExec(requestExec, connManager, reuseStrategy, keepAliveStrategy,
myProxyHttpProcessor, targetAuthStrategy, proxyAuthStrategy, userTokenHandler);
}
};
HttpClient 5.0经典版:
CloseableHttpClient httpClient = HttpClientBuilder.create()
.replaceExecInterceptor(ChainElement.CONNECT.name(),
new ConnectExec(
DefaultConnectionReuseStrategy.INSTANCE,
new DefaultHttpProcessor(new RequestTargetHost()),
DefaultAuthenticationStrategy.INSTANCE))
.build();
感谢您的帮助@ok2c:)不幸的是,HttpClient 4.5的问题仍然存在。但是,我已经用HttpClient 5.0进行了测试,它运行正常。我刚刚在新的DefaultHttpProcessor上添加了我的响应拦截器。@RAZAFINARIVOHanania HttpClient 4.5到底存在什么?这是同样的原理,只是有点丑陋的实现。我无法使用定制的HttpClientBuilder截获HttpClient 4.5的CONNECT响应。但是,它会拦截连接请求。但是在使用您的解决方案运行的HttpClient 5上,响应时调用了
process()
函数。我检查createMainExec()的功能。我看到它创建了MainClientExec
的新实例。在这个类中,我发现HttpProcessor只能使用一次,而且只能根据请求使用。有一行this.requestExecutor.preProcess(connect,this.proxyHttpProcessor,context)代码>但是当它得到响应时,proxyHttpProcessor就没有用处了。我认为这就是问题所在。