HTTP client in Java: making requests correctly from A to Z
In modern Java development, interacting with external web services and REST APIs has become an integral part of virtually any application. Whether you are writing a microservice, a mobile application, or an enterprise system, the ability to correctly send HTTP requests is a critically important skill. In this article, we will cover all the ways to create an HTTP client in Java: from the classic HttpURLConnection to modern libraries such as java.net.http.HttpClient, OkHttp, and RestTemplate. You will learn how to make GET, POST, PUT, and DELETE requests, handle responses, and choose the right tool for your task.
1. Classic approach: HttpURLConnection
For many years, the standard way to work with HTTP in Java was the HttpURLConnection class. It is part of the JDK and requires no additional dependencies. However, its API is considered low-level and cumbersome, especially for complex requests.
GET request example:
import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;
public class HttpURLConnectionExample { public static void main(String[] args) throws Exception { URL url = new URL("https://api.example.com/users"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", "application/json");
int responseCode = connection.getResponseCode(); System.out.println("Response Code: " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println("Response: " + response.toString()); }}POST request with JSON example:
import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;
public class PostExample { public static void main(String[] args) throws Exception { URL url = new URL("https://api.example.com/users"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true);
String jsonInputString = "{\\"name\\": \\"John\\", \\"age\\": 30}"; try (OutputStream os = connection.getOutputStream()) { byte[] input = jsonInputString.getBytes("utf-8"); os.write(input, 0, input.length); }
int responseCode = connection.getResponseCode(); System.out.println("Response Code: " + responseCode); }}Pros: built into the JDK, no libraries required.
Cons: complex code, no HTTP/2 support, inconvenient handling of timeouts and errors.
2. Modern HttpClient (Java 11+)
Starting with Java 11, a new java.net.http.HttpClient was added to the standard library. It supports HTTP/2, WebSocket, asynchronous requests, and provides a much cleaner and more modern API.
Synchronous GET request:
import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;
public class ModernHttpClientExample { public static void main(String[] args) throws Exception { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://a