Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customise "host" header in Java http client

Here's my 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());

As a result I see that the above code sends:

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

As you can see it sends two Host headers (the one from URI and the one I specified), but I would like it to send the Host header that I specified, not the one from the URI. Is it possible with this client?

EDIT: In Java 11, it gets even worse (you need to change the client.send line to: client.send(request, HttpResponse.BodyHandlers.ofString());):

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

How can I customize that header (needed for testing of virtual hosts)?

I also tried the setHeader and get exactly the same problem (either double Host headers, or the exception).

EDIT: I reported a JDK bug.

like image 674
Krzysztof Krasoń Avatar asked Sep 13 '18 14:09

Krzysztof Krasoń


People also ask

How do you add a header to an HTTP request in Java?

Instead of setting the Header on each and every request, we can also configure it as a default header on the Client itself: Header header = new BasicHeader(HttpHeaders. CONTENT_TYPE, "application/json"); List<Header> headers = Lists. newArrayList(header); HttpClient client = HttpClients.

Can I add custom header to HTTP request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

What is the Host HTTP header?

The HTTP host header is a request header that specifies the domain that a client (browser) wants to access. This header is necessary because it is pretty standard for servers to host websites and applications at the same IP address. However, they don't automatically know where to direct the request.

Why we use HttpHeaders in Java?

The HTTP headers are used to pass additional information between the clients and the server through the request and response header. All the headers are case-insensitive, headers fields are separated by colon, key-value pairs in clear-text string format.


Video Answer


1 Answers

As of Java 12 (EA build 22) it has been solved by additional property jdk.httpclient.allowRestrictedHeaders (see https://bugs.openjdk.java.net/browse/JDK-8213696).

So now one can override Host (or any other restricted header) by executing the code with: java -Djdk.httpclient.allowRestrictedHeaders=host ...

like image 109
Krzysztof Krasoń Avatar answered Sep 21 '22 23:09

Krzysztof Krasoń