gpt4 book ai didi

java - Apache HttpClient : how to send headers along with CONNECT request

转载 作者:行者123 更新时间:2023-12-02 04:33:23 24 4
gpt4 key购买 nike

我需要连接到读取 CONNECT 请求中 header 的代理。我特指的是在切换到 HTTPS 加密流之前与 CONNECT 一起传递的 header 。

这可以用 HttpClient 实现吗?它的默认行为似乎是通过加密流推送所有 header 。

最佳答案

我不确定您应该这样做(我个人没有看到向 CONNECT 请求添加自定义 header 的有效理由),但这就是使用 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();

关于java - Apache HttpClient : how to send headers along with CONNECT request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31157109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com