Modern HTTP client
Home / I/O / Modern HTTP client
Code Comparison
URL url = new URL("https://api.com/data");
HttpURLConnection con =
(HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
// read lines, close streams...
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.com/data"))
.build();
var response = client.send(
request, BodyHandlers.ofString());
String body = response.body();
Why the modern way wins
๐
Builder API
Fluent builder for requests, headers, and timeouts.
๐
HTTP/2 support
Built-in HTTP/2 with multiplexing and server push.
โก
Async ready
sendAsync() returns CompletableFuture.
Old Approach
HttpURLConnection
Modern Approach
HttpClient
JDK Support
Modern HTTP client
Available
Widely available since JDK 11 (Sept 2018)
How it works
HttpClient supports HTTP/1.1 and HTTP/2, async requests, WebSocket, custom executors, and connection pooling. No more casting URLConnection or manually reading InputStreams.
Related Documentation
Proof