我怎样才能覆盖“主机”使用 Apache commons HttpClient 时请求中的标头

发布于 2024-11-08 01:39:51 字数 336 浏览 0 评论 0 原文

我正在使用 Jakarta Commons HttpClient 3.1 编写一个负载测试工具,该工具需要针对不同的服务器,并假装它针对的是 HTTP 服务器中的正确虚拟主机。为此,我需要能够将请求中的“Host”HTTP 标头设置为不同的主机名,然后是我要连接的实际主机名。

看起来很明显我应该使用 Method.setRequestHeader("Host","fakehostname"),但 HttpClient 只是忽略这一点并始终发送我在“Host”中连接的真实主机名标头(我已经启用了“httpclient.wire”的调试日志记录,并且我可以专门执行此操作)。

如何覆盖标头以便 HttpClient 注意到?

I am using Jakarta Commons HttpClient 3.1 writing a load test tool that needs to target different servers and pretend like it targeted the correct virtual host in the HTTP server. For that I need to be able to set the "Host" HTTP header in the request to a different host name then the actual host name that I'm connecting to.

It seemed pretty obvious that I should use Method.setRequestHeader("Host","fakehostname"), but HttpClient just ignores this and always sends the real host name I'm connecting to in the "Host" header (I've enabled debug logging for "httpclient.wire" and I can it does this specifically).

How can I override the header so that HttpClient takes heed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

智商已欠费 2024-11-15 01:39:51

经过更多搜索,并从 Oleg 的答案中得到提示,我找到了方法 HttpMethodParams::setVirtualHost()

当 HttpClient 格式化请求时,它总是在发送请求之前创建“Host”标头本身 - 因此它不能被覆盖为标准标头。但在从 URL 生成“Host”标头的主机名之前,HttpClient 会检查 HttpMethodParams 对象以查看用户是否想要覆盖主机名。这只会覆盖主机名而不是端口,因此它会更容易使用,但不如我想要的那么直观。

使用它的代码可以如下所示:

Method m = new GetMethod("http://some-site/some/path");
m.getParams().setVirtualHost("some-other-site");
client.executeMethod(m);

因为我喜欢一个衬垫,所以也可以写为:

client.executeMethod(new GetMethod("http://some-site/some/path") {{
    getParams().setVirtualHost("some-other-site"); }});

After searching some more, and taking a hint from Oleg's answer, I've found the method HttpMethodParams::setVirtualHost().

when HttpClient formats a request, it always creates the "Host" header itself just before sending the request - so it cannot be overridden as a standard header. But before the host name for the "Host" header is generated from the URL, HttpClient checks the HttpMethodParams object to see if the user wants to override the host name. This only overrides the host name and not the port so it would be easier to use, though not as intuitive as I'd like.

The code to use this can look like this:

Method m = new GetMethod("http://some-site/some/path");
m.getParams().setVirtualHost("some-other-site");
client.executeMethod(m);

Because I like one liners, this can also be written as:

client.executeMethod(new GetMethod("http://some-site/some/path") {{
    getParams().setVirtualHost("some-other-site"); }});
浅暮の光 2024-11-15 01:39:51

我相信你想要 http://hc .apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpHost.html:这允许您为特定连接配置主机。如果我理解正确,您可以使用 execute 方法(请参阅 http://hc.apache.org/httpcomponents-client- ga/httpclient/apidocs/org/apache/http/impl/client/AbstractHttpClient.html#execute(org.apache.http.HttpHost,%20org.apache.http.HttpRequest)) 并传递给它一个自定义 < code>HttpHost 对象,或者执行以下操作:

  1. 构造一个 HttpHost 实例,将其传递给您的 Host 标头,
  2. 使用它来创建一个 HttpRoute 实例(请参阅 http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/ apache/http/conn/routing/HttpRoute.html
  3. 当您请求连接时将其传递给连接管理器(请参阅http://hc.apache .org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/conn/ClientConnectionManager.html#requestConnection(org.apache.http.conn.routing.HttpRoute,%20java.lang.Object) )。
  4. 使用您的方法的连接:请参阅 http://hc.apache .org/httpcomponents-client-ga/tutorial/html/connmgmt.html 了解更多详细信息。

让我知道这是如何运作的。

编辑:原则保持不变。
1. 构造一个 HttpHost 实例,向其传递您的 Host 标头(请参阅 http://hc.apache.org/httpclient-legacy/apidocs/index.html?org/apache/commons/httpclient/HostConfiguration.html)。
3. 使用该配置在 HttpClient 上使用 execute 方法(请参阅 http://hc.apache.org/httpclient-legacy/apidocs/org/ apache/commons/httpclient/HttpClient.html#executeMethod(org.apache.commons.httpclient.HostConfiguration,%20org.apache.commons.httpclient.HttpMethod))

I believe you want http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpHost.html: this lets you configure the host for a specific connection. If I understand it correctly, you can either use the execute method (see http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/AbstractHttpClient.html#execute(org.apache.http.HttpHost,%20org.apache.http.HttpRequest)) and pass it a custom HttpHost object, or do this:

  1. Construct an HttpHost instance, passing it your Host header.
  2. Use that to create an HttpRoute instance (see http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/conn/routing/HttpRoute.html)
  3. Pass that to the connection manager when you request a connection (see http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/conn/ClientConnectionManager.html#requestConnection(org.apache.http.conn.routing.HttpRoute,%20java.lang.Object)).
  4. Use the connection with your method: see http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html for more details.

Let me know how that works.

EDIT: principle remains the same.
1. Construct an HttpHost instance, passing it your Host header (see http://hc.apache.org/httpclient-legacy/apidocs/index.html?org/apache/commons/httpclient/HttpHost.html).
2. Create an HttpConfiguration instance and then pass it the HttpHost you created (see http://hc.apache.org/httpclient-legacy/apidocs/index.html?org/apache/commons/httpclient/HostConfiguration.html).
3. Use the execute method on HttpClient with that configuration (see http://hc.apache.org/httpclient-legacy/apidocs/org/apache/commons/httpclient/HttpClient.html#executeMethod(org.apache.commons.httpclient.HostConfiguration,%20org.apache.commons.httpclient.HttpMethod))

A君 2024-11-15 01:39:51

以下适用于 android:

System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
InputStream stream_content=null;
try
   {URL url=new URL("http://74.125.28.103/");
    HttpURLConnection conn=(HttpURLConnection)url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Host", "www.google.com");
    stream_content=conn.getInputStream();
   }
catch (Exception e) {}

https url:

System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
InputStream stream_content=null;
try
   {URL url=new URL("https://74.125.28.103/");
    HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
    conn.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER );
    conn.setDoOutput(true);
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Host", "www.google.com");
    stream_content=conn.getInputStream();
   }
catch (Exception e) {}

Following works on android:

System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
InputStream stream_content=null;
try
   {URL url=new URL("http://74.125.28.103/");
    HttpURLConnection conn=(HttpURLConnection)url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Host", "www.google.com");
    stream_content=conn.getInputStream();
   }
catch (Exception e) {}

for https url:

System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
InputStream stream_content=null;
try
   {URL url=new URL("https://74.125.28.103/");
    HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
    conn.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER );
    conn.setDoOutput(true);
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Host", "www.google.com");
    stream_content=conn.getInputStream();
   }
catch (Exception e) {}
南冥有猫 2024-11-15 01:39:51

可以使用 'http.virtual-host' 参数来强制将任意(虚拟)主机名和端口作为 Host 请求标头的值,而不是派生的值来自实际请求 URI。不过,这仅适用于 4.x API。

One can use the 'http.virtual-host' parameter in order to force an arbitrary (virtual) hostname and port as a value of the Host request header instead of those derived from the actual request URI. This works with the 4.x API only, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文