New HttpClient

Available

1.1 Introduction to HttpClient

Beginning with JDK 11, the developers of the Java platform added a powerful new tool to the JDK for making http requests, the java.net.http. It contains four key classes:

  • HttpClient
  • HttpRequest
  • HttpResponse
  • web socket

These are very powerful classes that allow you to perform all possible types of requests using the HTTP, HTTP/2and WebSocket.

In addition, you can use these classes to make both synchronous and asynchronous http requests.

Making an http request consists of the following parts:

  1. Create an objectHttpClient
  2. Create an objectHttpRequest
  3. Sending a request using the send()or methodsendAsync()
  4. Response processingHttpResponse

An example of such a request:

HttpClient client = HttpClient.newBuilder()
        .version(Version.HTTP_1_1)
        .followRedirects(Redirect.NORMAL)
        .connectTimeout(Duration.ofSeconds(20))
        .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)))
        .authenticator(Authenticator.getDefault())
        .build();

HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());

1.2 Declarative approach

In the example above, you see an example of the so-called declarative approach to writing code. Let's take a look at the first part of the example:

HttpClient client = HttpClient.newBuilder()
.version(Version.HTTP_1_1)
.followRedirects(Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(20))
.proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)))
.authenticator(Authenticator.getDefault())
.build();

What this code would look like written in classic style:

HttpClient client = HttpClient.new();
client.setVersion(Version.HTTP_1_1);
client.setFollowRedirects(Redirect.NORMAL);
client.setConnectTimeout(Duration.ofSeconds(20));
client.setProxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)));
client.setAuthenticator(Authenticator.getDefault());

When using the declarative approach in the code, two things change. Firstly , all class methods HttpClient return their own object , which allows you to organize code in the form of chains.

Classic code:
HttpClient client = HttpClient.new();
client.setVersion(Version.HTTP_1_1);
client.setFollowRedirects(Redirect.NORMAL);
client.setConnectTimeout(Duration.ofSeconds(20));
client.setAuthenticator(Authenticator.getDefault());
As a chain:
HttpClient client = HttpClient.new() .setVersion(Version.HTTP_1_1) .setFollowRedirects(Redirect.NORMAL). setConnectTimeout(Duration.ofSeconds(20)) .setAuthenticator(Authenticator.getDefault());
We transfer each method to a separate line (this is one long statement)
HttpClient client = HttpClient.new()
.setVersion(Version.HTTP_1_1)
.setFollowRedirects(Redirect.NORMAL)
.setConnectTimeout(Duration.ofSeconds(20))
.setAuthenticator(Authenticator.getDefault());

Secondly , the prefix is ​​removed from the methods set, which allows you to write code even more compactly:

Was
HttpClient client = HttpClient.new()
.setVersion(Version.HTTP_1_1)
.setFollowRedirects(Redirect.NORMAL)
.setConnectTimeout(Duration.ofSeconds(20))
.setAuthenticator(Authenticator.getDefault());
It became
HttpClient client = HttpClient.new()
.version(Version.HTTP_1_1)
.followRedirects(Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(20))
.authenticator(Authenticator.getDefault());

Such code is easier to read, although harder to write.

And one more important point. In this example, the Builder pattern was used. There are scenarios where creating an object is a complex process. Therefore, they prefer to formalize it: it starts with a conditional method call begin()and ends with a conditional method call end().

In the example that we analyzed, the method HttpClient.newBuilder()returns an object HttpClient.Builder(this is an internal utility class of the class HttpClient). All methods of the type version()are called just on this service object. Well, the call of the method build()marks the end of the construction of the object and returns the object HttpClient.

Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet