JDK-8210740 : Allow customization of Host header with HttpClient
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 11
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • Submitted: 2018-09-13
  • Updated: 2019-08-23
  • Resolved: 2019-08-23
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdResolved
Related Reports
Duplicate :  
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Debian Linux testing, 4.15.7 kernel.

openjdk 11 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)


A DESCRIPTION OF THE PROBLEM :
Host header is used in virtual host environments and it is important to be able to customize it.
Right now following code fails:
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.BodyHandler.asString());

With exception: java.lang.IllegalArgumentException: restricted header name: "Host"

(In JDK 10, it doesn't fail but sends two Host headers, one from URI and the second one from requested).

This should not be the case.
Host header should be customizable (like in other http client libraries - sometimes it is called virtualHost), and should override the one that exists in the URI.

Reported on SO:
  https://stackoverflow.com/questions/52315472/how-to-customise-host-header-in-java-http-client



Comments
This has been implemented by JDK-8213189. However, you would need to set the property -Djdk.httpclient.allowRestrictedHeaders=host either on the java command-line or in the net.properties file if you want to override the system default for this header.
23-08-2019

This has probably been fixed by JDK-8213189 but we need to double check that the "host" header isn't sent twice before closing as duplicate.
21-08-2019

Additional data point: http://mail.openjdk.java.net/pipermail/net-dev/2018-November/011904.html
05-11-2018

See also: https://stackoverflow.com/questions/52315472/how-to-customise-host-header-in-javas-http-client
14-09-2018