

You must set the URI request method and specify the body, timeout, and headers optionally. Similar to clients, you build HttpRequest instances with a builder. newHttpClient ( ) // equivalentĬlient = HttpClient.

If you are okay with the default settings, you can build the client with newHttpClient(). See the examples below for more information about followRedirects(), authenticator() and cookieHandler(). A default executor is created for each newly built HttpClient if you don't specify an executor. If the connection can't be established, the client throws a HttpConnectTimeoutException exception.Įxecutor() sets the executor to be used for asynchronous and dependent tasks. If you know in advance that the server only speaks HTTP/1.1, you may create the client with version(Version.HTTP_1_1).ĬonnectTimeout() determines how long the client waits until a connection can be established. If the server answers with HTTP/1.1, the client automatically falls back to this version. Once created, an HttpClient instance is immutable, thus automatically thread-safe, and you can send multiple requests with it.īy default, the client tries to open an HTTP/2 connection. Here is an example that shows all available settings. You can configure per-client settings when building the client. You first have to create an HttpClient with a builder-style API to send requests. Visit the JavaDoc to see a complete overview: This is not a complete list of all available classes and interfaces. The Java 11 HTTP client is part of the Java SE platform and comprises the following classes and interfaces that all reside in the package (module: ).

This is important for the client part of the demos because they trust this private root CA. Make sure that you set the environment variable JAVA_HOME correctly before running mkcert -install, so mkcert can add the root CA to the Java installation.
#Java async http client install
I utilized mkcert to install a private CA and to create the TLS certificate ( see my previous blog post). All the other examples also work with HTTP/1.1 and cleartext HTTP. Only the HTTP/2 Server Push demo requires HTTP/2 and TLS.
#Java async http client code
You find the code for this application on GitHub:Īll examples use HTTP/2 and TLS, but you can disable it in application.properties. Most examples speak with a local Spring Boot server. In the following article, I show you multiple examples of the library. You find examples of these features in the demo applications below.

However, these missing features are no deal-breakers and can be implemented on top of the new HTTP client library, which speaks for the flexibility and configurability of the library. Especially the missing compression support surprised me a little bit coming from OkHttp, where the library transparently handles this functionality. I think there is still room for these libraries on Java 11 because, as you will see later, the new Java 11 HTTP client misses a few convenient functions like URI builder, multipart form data, form data, and compression support. Because of that, most projects added an external HTTP client library, like Apache HTTP Client and OkHttp to their projects. However, it's not the most straightforward API to work with and does not support the newer HTTP/2 protocol. Up to Java version 11, the only built-in way to work with HTTP is URLConnection (available since Java 1.0). It's a way for the Java platform developers to release new features early, collect feedback from the developer community, and, when ready, release it officially, as they did with the HTTP client library. We may see more of this pattern in the future because of Java's shorter six-month release cycles. To be exact, the library was already part of Java 9 but only as an incubation module. It's one of the more significant new features we've got with Java 11. In this blog post, we will look at the HTTP client library introduced in Java 11 (September 2018). A closer look at the Java 11 HTTP Client Home | Send Feedback A closer look at the Java 11 HTTP Client
