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/2
and 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:
- Create an object
HttpClient
- Create an object
HttpRequest
- Sending a request using the
send()
or methodsendAsync()
- Response processing
HttpResponse
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: |
---|
|
As a chain: |
|
We transfer each method to a separate line (this is one long statement) |
|
Secondly , the prefix is removed from the methods set
, which allows you to write code even more compactly:
Was |
---|
|
It became |
|
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
.
GO TO FULL VERSION