I/O Beginner

Use the built-in HttpClient for clean, modern HTTP requests.

✕ 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();
See a problem with this code? Let us know.
📐

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
Modern HTTP client
Available

Widely available since JDK 11 (Sept 2018)

HttpClient supports HTTP/1.1 and HTTP/2, async requests, WebSocket, custom executors, and connection pooling. No more casting URLConnection or manually reading InputStreams.