先决条件
问题
我尝试通过http4组件调用https加密的站点。在我的服务器和Internet(目标服务器)之间的代理检查标头“ User-Agent”,如果请求为空,则拒绝该请求。
connect-Request不包含http头“ User-Agent”。
在org.apache.http.impl.execchain.MainClientExec中,private boolean createTunnelToTarget(AuthState proxyAuthState, HttpClientConnection managedConn, HttpRoute route, HttpRequest request, HttpClientContext context) throws HttpException, IOException
以下方法被调用:
BasicHttpRequest connect = new BasicHttpRequest("CONNECT", authority, request.getProtocolVersion());
this.requestExecutor.preProcess(connect, this.proxyHttpProcessor, context);
this.requestExecutor.preProcess
将标头“ Host”和“ Proxy-Connection”添加到HTTP CONNECT请求中,但不添加“ User-Agent”。
如何将标头“ User-Agent”添加到HTTP CONNECT请求中?
问候,
最大限度
该问题已在httpclient 4.4.1中解决。
在类org.apache.http.impl.client.HttpClientBuilder中,使用针对用户代理的HttpRequestInterceptor初始化httpprocessor:
ClientExecChain execChain = createMainExec(
requestExecCopy,
connManagerCopy,
reuseStrategyCopy,
keepAliveStrategyCopy,
new ImmutableHttpProcessor(new RequestTargetHost(), new RequestUserAgent(userAgentCopy)),
targetAuthStrategyCopy,
proxyAuthStrategyCopy,
userTokenHandlerCopy);
解决方案是将httpclient版本从4.3.3更新到4.4.1。在Maven中,我必须定义依赖项,以便不选择骆驼使用的版本。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句