Java ApacheHttpClient:如何随连接请求一起发送头
我需要连接到一个代理,该代理读取连接请求中的头。我指的是在切换到en-HTTPS加密流之前,随CONNECT一起传递的头文件Java ApacheHttpClient:如何随连接请求一起发送头,java,apache-httpclient-4.x,apache-commons,apache-commons-httpclient,Java,Apache Httpclient 4.x,Apache Commons,Apache Commons Httpclient,我需要连接到一个代理,该代理读取连接请求中的头。我指的是在切换到en-HTTPS加密流之前,随CONNECT一起传递的头文件 HttpClient是否可能实现这一点?它的默认行为似乎是通过加密流推送所有头。我不确定您是否应该这样做(我个人不认为添加自定义头来连接请求的有效原因),但这是使用HttpClient 4.3或更高版本可以做到的 class MyHttpClientBuilder extends HttpClientBuilder { @Override protect
HttpClient是否可能实现这一点?它的默认行为似乎是通过加密流推送所有头。我不确定您是否应该这样做(我个人不认为添加自定义头来连接请求的有效原因),但这是使用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();
非常感谢!为什么这样做:我正在与之交谈的代理需要一些指令来完成其工作。对于常规HTTP请求,它获取头并将其从中继请求中剥离。对于HTTPS请求,除了发送连接头之外没有其他方法。可能有人对HTTP协议太有创意了。我接受了你的答案,但它似乎不起作用。头被添加,但仅添加到加密流中。知道吗?不知道。正常请求(您称之为加密流)的协议处理器在ProtocolExec中初始化。MainExec使用的协议侦听器仅用于处理传出连接请求