Modern HTTP client
Use the built-in HttpClient for clean, modern HTTP requests.
Code Comparison
✕ Java 8
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...
✓ Java 11+
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
Since JDK
11
Difficulty
beginner
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.