Reputation: 463
i use an http proxy to connect on an https website through CloseableHttpClient. A CONNECT request is sent first because it must do a tunnel connection. There will be response headers sent and i need to retrieve them. Result will be stored in a variable and used later.
On HttpClient 4.2, i can achieve it using an HttpResponseInterceptor :
final DefaultHttpClient httpClient = new DefaultHttpClient();
//configure proxy
httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
final Header[] headers = httpResponse.getAllHeaders();
//store headers
}
});
But when i use HttpClient 4.5, my response interceptor is never called on CONNECT request. It jumps directly on GET or POST request :
final CloseableHttpClient httpClient = HttpClients.custom()
.setProxy(new HttpHost(proxyHost, proxyPort, "http"))
.addInterceptorFirst(new HttpResponseInterceptor() {
public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
final Header[] headers = httpResponse.getAllHeaders();
//store headers
}
}).build();
Same result for methods setInterceptorLast()
and setHttpProcessor()
.
Thanks in advance :)
Upvotes: 2
Views: 885
Reputation: 27538
You will need a custom HttpClientBuilder
class in order to be able to add custom protocol interceptor to the HTTP protocol processor used to execute CONNECT
methods.
HttpClient 4.5 It is not pretty but it will get the job done:
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 classic:
CloseableHttpClient httpClient = HttpClientBuilder.create()
.replaceExecInterceptor(ChainElement.CONNECT.name(),
new ConnectExec(
DefaultConnectionReuseStrategy.INSTANCE,
new DefaultHttpProcessor(new RequestTargetHost()),
DefaultAuthenticationStrategy.INSTANCE))
.build();
Upvotes: 1