Java 添加http头";“用户代理”;发送HTTP连接请求

Java 添加http头";“用户代理”;发送HTTP连接请求,java,http,apache-camel,apache-httpclient-4.x,Java,Http,Apache Camel,Apache Httpclient 4.x,先决条件 ApacheTomcat7 弹簧3.2.11.释放 ApacheCamel 2.14.1 Camel HTTP4端点(Camel-HTTP4) 问题 我尝试通过http4组件调用https加密的站点。 位于我的服务器和internet(目标服务器)之间的代理检查标头“User Agent”,如果该标头为空,则拒绝该请求 连接请求不包含http头“用户代理” 在org.apache.http.impl.execchain.MainClientExec中,方法私有布尔createTun

先决条件

  • ApacheTomcat7
  • 弹簧3.2.11.释放
  • ApacheCamel 2.14.1
  • Camel HTTP4端点(Camel-HTTP4)
问题

我尝试通过http4组件调用https加密的站点。 位于我的服务器和internet(目标服务器)之间的代理检查标头“User Agent”,如果该标头为空,则拒绝该请求

连接请求不包含http头“用户代理”

在org.apache.http.impl.execchain.MainClientExec中,方法
私有布尔createTunnelToTarget(AuthState proxyAuthState、HttpClientConnection managedConn、HttpRoute路由、HttpRequest请求、HttpClientContext上下文)抛出HttpException,调用IOException

    BasicHttpRequest connect = new BasicHttpRequest("CONNECT", authority, request.getProtocolVersion());
    this.requestExecutor.preProcess(connect, this.proxyHttpProcessor, context);
this.requestExecutor.preProcess
将头“主机”和“代理连接”添加到HTTP连接请求中,但不添加“用户代理”

如何将标题“用户代理”添加到HTTP连接请求

问候,


Max

该问题在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中,我必须定义依赖项,以便不选择camel使用的verson