Apache HttpClient:如何发送 headers 连同 CONNECT 请求
Apache HttpClient: how to send headers along with CONNECT request
我需要连接到在 CONNECT 请求中读取 headers 的代理。我的意思是在切换到 en HTTPS-encrypted 流之前,与 CONNECT 一起传递的 headers。
HttpClient 可以吗?它的默认行为似乎是通过加密流推送所有 headers。
我不确定你是否应该这样做(我个人看不到向 CONNECT 请求添加自定义 headers 的正当理由)但这是使用 HttpClient 4.3 或更新版本可以做到的
class MyHttpClientBuilder extends HttpClientBuilder {
@Override
protected ClientExecChain createMainExec(
final HttpRequestExecutor requestExec,
final HttpClientConnectionManager connManager,
final ConnectionReuseStrategy reuseStrategy,
final ConnectionKeepAliveStrategy keepAliveStrategy,
final HttpProcessor proxyHttpProcessor,
final AuthenticationStrategy targetAuthStrategy,
final AuthenticationStrategy proxyAuthStrategy,
final UserTokenHandler userTokenHandler) {
final HttpProcessor myProxyHttpProcessor = new ImmutableHttpProcessor(new RequestTargetHost(), new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
request.addHeader("Hello", "Mom says hi");
}
});
return super.createMainExec(requestExec, connManager, reuseStrategy, keepAliveStrategy,
myProxyHttpProcessor, targetAuthStrategy, proxyAuthStrategy, userTokenHandler);
}
}
HttpClientBuilder httpClientBuilder = new MyHttpClientBuilder();
CloseableHttpClient client = MyHttpClientBuilder.build();
我需要连接到在 CONNECT 请求中读取 headers 的代理。我的意思是在切换到 en HTTPS-encrypted 流之前,与 CONNECT 一起传递的 headers。
HttpClient 可以吗?它的默认行为似乎是通过加密流推送所有 headers。
我不确定你是否应该这样做(我个人看不到向 CONNECT 请求添加自定义 headers 的正当理由)但这是使用 HttpClient 4.3 或更新版本可以做到的
class MyHttpClientBuilder extends HttpClientBuilder {
@Override
protected ClientExecChain createMainExec(
final HttpRequestExecutor requestExec,
final HttpClientConnectionManager connManager,
final ConnectionReuseStrategy reuseStrategy,
final ConnectionKeepAliveStrategy keepAliveStrategy,
final HttpProcessor proxyHttpProcessor,
final AuthenticationStrategy targetAuthStrategy,
final AuthenticationStrategy proxyAuthStrategy,
final UserTokenHandler userTokenHandler) {
final HttpProcessor myProxyHttpProcessor = new ImmutableHttpProcessor(new RequestTargetHost(), new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
request.addHeader("Hello", "Mom says hi");
}
});
return super.createMainExec(requestExec, connManager, reuseStrategy, keepAliveStrategy,
myProxyHttpProcessor, targetAuthStrategy, proxyAuthStrategy, userTokenHandler);
}
}
HttpClientBuilder httpClientBuilder = new MyHttpClientBuilder();
CloseableHttpClient client = MyHttpClientBuilder.build();