我正在使用 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?
发布评论
评论(4)
经过更多搜索,并从 Oleg 的答案中得到提示,我找到了方法 HttpMethodParams::setVirtualHost()。
当 HttpClient 格式化请求时,它总是在发送请求之前创建“Host”标头本身 - 因此它不能被覆盖为标准标头。但在从 URL 生成“Host”标头的主机名之前,HttpClient 会检查 HttpMethodParams 对象以查看用户是否想要覆盖主机名。这只会覆盖主机名而不是端口,因此它会更容易使用,但不如我想要的那么直观。
使用它的代码可以如下所示:
因为我喜欢一个衬垫,所以也可以写为:
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:
Because I like one liners, this can also be written as:
我相信你想要 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 标头(请参阅 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 customHttpHost
object, or do this: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))以下适用于 android:
https url:
Following works on android:
for https url:
可以使用
'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 theHost
request header instead of those derived from the actual request URI. This works with the 4.x API only, though.