-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJEP517HTTP3ForTheHTTPClientAPI.java
More file actions
36 lines (31 loc) · 1.46 KB
/
JEP517HTTP3ForTheHTTPClientAPI.java
File metadata and controls
36 lines (31 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpClient.Version;
public class JEP517HTTP3ForTheHTTPClientAPI {
static void main() {
try {
// 1. Configure the HttpClient to prefer HTTP/3
// If the server does not support HTTP/3, it will automatically fallback to HTTP/2
HttpClient client = HttpClient.newBuilder()
.version(Version.HTTP_3)
.build();
// 2. Create the request for an HTTP/3 enabled endpoint
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://quic.rocks:4433/"))
.GET()
.build();
// 3. Send the request and receive the response synchronously
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// 4. Output the results to the console
System.out.println("Protocol Used: " + response.version());
System.out.println("Status Code: " + response.statusCode());
System.out.println("Response Body (Preview): " + response.body().substring(0, 100) + "...");
} catch (Exception e) {
// Handle potential network or protocol exceptions
System.err.println("Error during HTTP/3 request: " + e.getMessage());
e.printStackTrace();
}
}
}