简体   繁体   English

将HTTP标头“ User-Agent”添加到HTTP CONNECT请求

[英]Add http header “User-Agent” to HTTP CONNECT request

Prerequisites 先决条件

  • Apache Tomcat 7 Apache Tomcat 7
  • Spring 3.2.11.RELEASE 春天3.2.11。发布
  • Apache Camel 2.14.1 阿帕奇骆驼2.14.1
  • Camel HTTP4 Endpoint (camel-http4) 骆驼HTTP4端点(camel-http4)

Problem 问题

I try to call a https encrypted site via http4 component. 我尝试通过http4组件调用https加密的站点。 The proxy which is in between my server and the internet (target server) checks the header "User-Agent" and rejects the request if it is empty. 在我的服务器和Internet(目标服务器)之间的代理检查标头“ User-Agent”,如果请求为空,则拒绝该请求。

The connect-Request does not contain the http header "User-Agent". connect-Request不包含http头“ User-Agent”。

In org.apache.http.impl.execchain.MainClientExec in method private boolean createTunnelToTarget(AuthState proxyAuthState, HttpClientConnection managedConn, HttpRoute route, HttpRequest request, HttpClientContext context) throws HttpException, IOException following is called: 在org.apache.http.impl.execchain.MainClientExec中,方法private boolean createTunnelToTarget(AuthState proxyAuthState, HttpClientConnection managedConn, HttpRoute route, HttpRequest request, HttpClientContext context) throws HttpException, IOException以下是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 adds header "Host" and "Proxy-Connection" to the HTTP CONNECT request but not "User-Agent". this.requestExecutor.preProcess将标头“ Host”和“ Proxy-Connection”添加到HTTP CONNECT请求中,但未添加“ User-Agent”。

How can I add the Header "User-Agent" to the HTTP CONNECT request? 如何将标头“ User-Agent”添加到HTTP CONNECT请求?

Regards, 问候,

Max 马克斯

The problem is solved in httpclient 4.4.1. 该问题已在httpclient 4.4.1中解决。

In class org.apache.http.impl.client.HttpClientBuilder the httpprocessor is initialzed with a HttpRequestInterceptor for User-Agent: 在类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);

The solution is to update the httpclient version from 4.3.3 to 4.4.1 . 解决方案是将httpclient版本从4.3.3更新到4.4.1。 In maven I had to define the dependency so that not the verson used by camel was choosen. 在Maven中,我必须定义依赖项,以便不选择骆驼使用的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能设置Java http User-Agent? - Why can't I set Java http User-Agent? 如何在Vaadin中设置用户代理标头? - How to set user-agent header in Vaadin? 在 Java 中的 HTTP 请求中添加 HEADER - Add HEADER in HTTP Request in Java 当我进行 http 后调用时如何删除用户代理字符串? - How to remove User-Agent String when I make a http post call? 如何在不使用HTTP POST进行用户代理交互的情况下发送AuthnRequest? - How to send AuthnRequest without user-agent interaction using HTTP POST? 尽管设置了User-Agent,但来自Java应用程序的HTTP 403(但不是Web浏览器) - HTTP 403 from Java app (but not web browser), despite User-Agent set 如何使用 apache http 客户端库 4.1 设置自定义用户代理? - How to set custom User-Agent with apache http client library 4.1? 谁能帮助您以编程方式使用Selenium Webdriver将修改标头用户代理添加到Chrome浏览器 - Can anyone help to how to add modify header user-agent to chrome browser using selenium webdriver programmatically 在HTTP请求标头中插入用户凭据 - Inserting user credentials in HTTP request header 无法从标题正确获取“ User-Agent” - Unable to get `User-Agent` properly from header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM