title: How to customise "host" header in Java http client
body: <p>Here's my code:</p>
  
  <pre><code>HttpClient client = HttpClient.newHttpClient();
  HttpRequest request = HttpRequest.newBuilder()
      .uri(URI.create("http://127.0.0.1:8081/"))
      .header("Host", "test.example.com")
      .build();
  client.send(request, HttpResponse.BodyHandlers.ofString());
  </code></pre>
  
  <p>As a result I see that the above code sends:</p>
  
  <pre><code>GET / HTTP/1.1
  Connection: Upgrade, HTTP2-Settings
  Content-Length: 0
  Host: 127.0.0.1:8081
  HTTP2-Settings: AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA
  Upgrade: h2c
  User-Agent: Java-http-client/10
  Host: test.example.com
  </code></pre>
  
  <p>As you can see it sends two <code>Host</code> headers (the one from URI and the one I specified), but I would like it to send the <code>Host</code> header that I specified, not the one from the URI.
  Is it possible with this client?</p>
  
  <p>EDIT: In Java 11, it gets even worse (you need to change the client.send line to: <code>client.send(request, HttpResponse.BodyHandlers.ofString());</code>):</p>
  
  <pre><code>java.lang.IllegalArgumentException: restricted header name: "Host"
  </code></pre>
  
  <p>How can I customize that header (needed for testing of virtual hosts)?</p>
  
  <p>I also tried the <code>setHeader</code> and get exactly the same problem (either double <code>Host</code> headers, or the exception).</p>
  
  <p>EDIT: I reported a <a href="https://bugs.openjdk.java.net/browse/JDK-8210740" rel="noreferrer">JDK bug</a>.</p>