How can I override the "Host" header in a request when using Apache HttpClient shares

I use Jakarta Commons HttpClient 3.1, a writing load testing tool that should aim at different servers and pretend that it is targeting the correct virtual host on the HTTP server. To do this, I need to configure the "Host" HTTP header in the request for a different host name, and then to the actual host name with which I am connecting.

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

How can I override the header so that HttpClient takes into account?

+7
source share
4 answers

After searching for a little more, and, getting a hint from Olegโ€™s answer, I found the HttpMethodParams :: setVirtualHost () method.

when the HttpClient formats the request, it always creates a "Host" header immediately before sending the request, so it cannot be redefined as a standard header. But before the host name for the "Host" header is created from the URL, the HttpClient checks the HttpMethodParams object to see if the user wants to redefine the host name. This only cancels the host name, not the port, so it will be easier to use, although not as intuitively as we would like.

The code to use might look like this:

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

I believe you want http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpHost.html : this allows you to configure the host for a specific connection. If I understand it correctly, you can 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 the following:

Let me know how it works.

EDIT: The principle remains the same. 1. Build an instance of HttpHost by passing it the Host header (see http://hc.apache.org/httpclient-legacy/apidocs/index.html?org/apache/commons/httpclient/HttpHost.html ). 2. Create an instance of HttpConfiguration and pass it the HttpHost 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 this 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 ))

+3
source

After working 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) {} 
+3
source

You can use the 'http.virtual-host' parameter to set the arbitrary (virtual) host name and port as the value of the Host request header instead of the data received from the URI of the real request. This only works with API 4.x.

+2
source

All Articles