HTTP client in Java: making requests correctly from A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: why do you need an HTTP client in Java?

\n\n

Modern Java development rarely goes without interaction with external services. Whether it's sending data to a server, retrieving information from a REST API, or integrating with cloud platforms — an HTTP client is required everywhere. In Java, there are several ways to perform an HTTP request, and it's important to choose the right approach.

\n\n

For a long time, the standard tool was the java.net.HttpURLConnection library, but with the release of Java 11, a new, more convenient module java.net.http appeared. In this article, we will cover all stages of working with an HTTP client in Java: from simple GET requests to sending JSON and error handling.

\n\n

The article is aimed at developers who are already familiar with the basics of Java but want to systematize their knowledge about network requests. We will use the built-in HttpClient from Java 11+, as this is the modern and recommended approach.

\n\n

1. HTTP client basics: creation and configuration

\n\n

Before sending requests, you need to create an instance of HttpClient. This is the central object through which all HTTP operations are performed. Let's look at the basic configuration.

\n\n

Creating a client

\n\n

The simplest way is to use the static method newHttpClient():

\n\n
import java.net.http.HttpClient;\n\nHttpClient client = HttpClient.newHttpClient();
\n\n

However, for real projects, configuration of timeouts, protocol version, or proxy is often required. Here is an example of advanced configuration:

\n\n
import java.net.http.HttpClient;\nimport java.time.Duration;\n\nHttpClient client = HttpClient.newBuilder()\n    .version(HttpClient.Version.HTTP_2) // use HTTP/2\n    .connectTimeout(Duration.ofSeconds(10)) // connection timeout\n    .followRedirects(HttpClient.Redirect.NORMAL) // automatic redirect following\n    .build();
\n\n

Important configuration parameters

\n\n
    \n
  • version — choice between HTTP/1.1 and HTTP/2 (default is HTTP/2 if the server supports it)
  • \n
  • connectTimeout — maximum connection waiting time
  • \n
  • followRedirects — redirect handling policy (NEVER, ALWAYS, NORMAL)
  • \n
  • proxy — proxy server configuration (if required)
  • \n
\n\n

Note: one instance of HttpClient can be used for many requests. It is thread-safe, so you can create it once and reuse it throughout the application.

\n\n

2. Creating and sending HTTP requests

\n\n

Now let's move on to the main part — forming requests. In Java 11+, the HttpRequest class is used for this. Let's look at the main types of requests.

\n\n

GET request: retrieving data

\n\n

GET is the simplest type of request. It is used to retrieve data from the server. Example:

\n\n
import java.net.URI;\nimport java.net.http.HttpRequest;\nimport java.net.http.HttpResponse;\n\nHttpRequest request = HttpRequest.newBuilder()\n    .uri(URI.create("https://api.example.com/users"))\n    .GET() // explicitly specify the method (can be omitted, GET is default)\n    .build();\n\nHttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());\n\nSystem.out.println("Status: " + response.statusCode());\nSystem.out.println("Response body: " + response.body());
\n\n

The send() method performs a synchronous request. If asynchronous execution is needed, use sendAsync(), which returns a CompletableFuture.

\n\n

POST request: sending data

\n\n

POST is used to send data to the server. Most often this is JSON or form data. Let's look at both options.

\n\n

POST with JSON:

\n\n
import java.net.http.HttpRequest.BodyPublishers;\n\nString json = "{\\"name\\":\\"Ivan\\",\\"email\\":\\"iva                

Blogs

Book Recommendations