title: Configuring Apache HttpClient to access service through proxy/load-balancer (overriding Host header)
body: <p>I am having a problem getting the Apache HttpClient to connect to a service external to my virtualised development environment. 
  To access the internet (e.g. api.twitter.com) I need to call a local URL (e.g. api.twitter.com.dev.mycompany.net), which then forwards the request to real host. </p>
  
  <p>The problem is, that to whatever request I send, I get a 404 Not Found response.</p>
  
  <p>I have tried debugging it using wget, and it appears the problem is, that the destination server identifies the desired resource by using both the request URL and the hostname in the Host header. Since the hostname does not match, it is unable to locate the resource.</p>
  
  <p>I have (unsuccessfully) tried to override the Host header by setting the http.virtual-host parameter on the client like this:</p>
  
  <pre><code>HttpClient client = new DefaultHttpClient();
  if (envType.isWithProxy()) {
      client.getParams().setParameter(ClientPNames.VIRTUAL_HOST, "api.twitter.com");
  }
  </code></pre>
  
  <p>Technical details:</p>
  
  <ol>
  <li><p>Client is used as an executor in RESTeasy to call the REST API. So "manually" setting the virtual host (as described <a href="https://stackoverflow.com/questions/6045911/how-can-i-override-the-host-header-in-the-request-when-using-apache-commons-ht">here</a>) is not an option. </p></li>
  <li><p>Everything is done via HTTPS/SSL - not that I think it makes a difference.</p></li>
  </ol>
  
  <p><strong>Edit 1:</strong> Using a HttpHost instead of a String does not have the desired effect either:</p>
  
  <pre><code>HttpClient client = new DefaultHttpClient();
  if (envType.isWithProxy()) {
      HttpHost realHost = new HttpHost("api.twitter.com", port, scheme);
      client.getParams().setParameter(ClientPNames.VIRTUAL_HOST, realHost);
  }
  </code></pre>
  
  <p><strong>Edit 2:</strong> Further investigation has revealed, that the parameter needs to be set on the request object. The following is the code v. 4.2-aplha1 of HttpClient setting the virtual host:</p>
  
  <pre><code>HttpRequest orig = request;
  RequestWrapper origWrapper = wrapRequest(orig);
  origWrapper.setParams(params);
  HttpRoute origRoute = determineRoute(target, origWrapper, context);
  
  virtualHost = (HttpHost) orig.getParams().getParameter(
              ClientPNames.VIRTUAL_HOST);
  </code></pre>
  
  <p><code>params</code>are the parameters passed from the client. But the value for 'virtualHost' is read from the request parameters.</p>
  
  <p>So this changes the nature of the question to: How do I set the VIRTUAL_HOST property on the requests?</p>