Java 11 HTTP Client Example

Java 11 introduced the HTTP Client which can be used to send requests over the network and retrieves their responses. HTTP Client replaces the HttpUrlConnection class as it is considered old and doesn’t support ease of use. HTTP Client API supports both HTTP/1.1 and HTTP/2.


HttpClient is also immutable, meaning it can be used to send multiple requests.

Each HttpRequest must be supplied with a BodyHandler and its (BodyHandler) function is to determine how to handle the response if there is any.

Requests can be either sent synchronously or asynchronously

HTTP Client, java example


  • allows inspection of the response code before the actual response body has been received


BodyHandlers exist for the sole purpose of handling response body types. Some examples:

  • BodyHandlers.ofByteArray()
  • BodyHandlers.ofString()
  • BodyHandlers.ofFile()
  • BodyHandlers.ofInputStream()
  • etc.

Data as reactive-streams

  • The HttpClient is effectively a Subscriber of request body and a Publisher of response body bytes
  • The request and response bodies are exposed as reactive streams (streams of data with non-blocking back pressure)


  • Converts Java objects into byte buffers suitable for sending as a request body


  • BodyPublishers::ofByteArray(byte[])
  • BodyPublishers::ofFIle(Path)
  • BodyPublishers::ofString(String)
  • etc.


  • consumes response body bytes and converts them into a Java type

With that out of the way, let’s see some implementation. First, we will send a GET request:

HttpRequest request = HttpRequest.newBuilder()

Now, let’s send an HTTP Request. I will show you both the synchronous and asynchronous examples:

Synchronous (blocks until HttpResponse is available) GET

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpClient.newBuilder()
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());

Asynchronous GET

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
client.sendAsync(request, BodyHandlers.ofString())


HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
HttpResponse<?> response = client.send(request, discarding());


The HTTP Client’s role is to replace the URLConnection API and is standardized in Java 11 as part of the Java SE Platform and is located in the package. One key advantage is that it uses modern Java language as well as API features.

1 4 votes
Article Rating
Inline Feedbacks
View all comments