The HttpClient
introduced in Java 11 provides an easy-to-use mechanism for sending HTTP requests, and it supports HTTP/2 out of the box. To utilize the HTTP/2 protocol with Java 11’s HttpClient
, you simply need to set the version
to HttpClient.Version.HTTP_2
while building the client.
Here’s how you can use it:
Step-by-Step Example
Below is an example of how to configure and use HttpClient
with HTTP/2 for making a GET request:
package org.kodejava.net.http;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Http2Example {
public static void main(String[] args) {
// Create an HttpClient with HTTP/2 enabled
HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.build();
// Create an HttpRequest to URL that supports HTTP/2
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://http2.golang.org"))
.GET()
.build();
try {
// Send the request and get the response
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
// Print response details
System.out.println("HTTP Version: " + response.version());
System.out.println("Status Code: " + response.statusCode());
System.out.println("Response Body:\n" + response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Explanation of the Code
- HttpClient Setup:
HttpClient.newBuilder()
creates a newHttpClient
instance.version(HttpClient.Version.HTTP_2)
specifies that HTTP/2 should be used as the preferred protocol. If the server cannot support HTTP/2, it will automatically fall back to HTTP/1.1.
- HttpRequest:
- A request is created using
HttpRequest.newBuilder()
with the desired URI and HTTP method (e.g., GET, POST, etc.). - For HTTP/2 compatibility, ensure the server supports HTTP/2.
- A request is created using
- HttpResponse:
httpClient.send(request, HttpResponse.BodyHandlers.ofString())
sends a synchronous request and waits for the server response. It returns anHttpResponse
object containing the status code, version used, headers, and body.
- Response Details:
- Use
response.version()
to inspect the actual protocol version used (HTTP/1.1 or HTTP/2). - Retrieve response data using
response.body()
.
- Use
Key Features:
- HTTP/2 is enabled by default if supported by the server.
- Asynchronous requests can also be made using the
sendAsync()
method:
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
Requirements:
- Ensure the server supports HTTP/2. If it doesn’t, the client will fall back to HTTP/1.1.
- Java 11 or later must be installed to use the
java.net.http
package.
Tips:
- Use tools like
Wireshark
or browser developer tools (via Network tab) to verify that HTTP/2 is being utilized. - Use
response.version()
to check the actual protocol version if in doubt about server support.
This code provides a clean and modern approach to handling HTTP/2 communication with Java