BlogPost
15.03.2023

A comprehensive introduction to java 11 new http client

Java 11 was released on September 25, 2018, and is the second long-term support release in the new deployment cycle, following Java 9 and Java 10. This new release brought plenty of changes and additions to its commercial aspect as well as new features and improvements for developers to embrace. A number of enhancements were made to the HTTP client, the commonly used protocol for communicating between applications that publish their capabilities in the form of a REST API. In this article, we’ll discuss how HTTP calls were made before Java 11, and its limitations then we’ll introduce the new client and its functionalities.
4

In the pre-Java 11 eras, a wide range of JDKs shipped with a built-in HTTP client, the standard way to make HTTP requests was to use HttpURLConnection, which was part of the java.net package. This implementation provides a basic API for making HTTP requests, but it had several limitations. HttpURLConnection was a blocking API, meaning the application freezes while the request is being sent and resumes only when the response was received, additionally, it was lacking functionalities to have fine-grain control over connections, such as connection pooling and connection idle. Furthermore, it didn’t have support for WebSockets, this API was not only built to use multiple protocols that are now not functioning, but it was also less intuitive to use and less user-friendly compared to other available third-party modules.

As of Java 11, a new implementation officially became part of the JDK, it was initially introduced in Java 9 under the name of HttpClient. This client aims the replace the old API with a more user-friendly, intuitive usage and provides several improvements over the old client. Unlike HttpURLConnection, HttpClient provides synchronous and asynchronous request mechanisms, which is a considerable enhancement, meaning with asynchronous execution, the application will not be blocked while the request is made, once a response was received it’s also being processed asynchronously, underneath, it takes great advantage of the new concurrency improvements that were introduced in java 8, namely CompletableFuture, which supports composable asynchronous programming. It also has features for connection pooling, which is managed internally, by doing this the client reduces the overhead of establishing a connection for each request. Unlike the old API, it supports WebSockets, enabling the development of real-time applications. Finally, HttpClient is designed to be more performant and efficient than HttpURLConnection.

Overall, HttpClient is a more modern and efficient way to make HTTP requests in Java, compared to the legacy HttpURLConnection API. It provides a more flexible, user-friendly, and performant way to make HTTP requests in Java, making it the preferred choice for new development.

sofiane
Sofiane BRANECI - Consultant Canopee
“ Diplômé de la Sorbonne, Sofiane s’est spécialisé dans la technologie Java et a eu l’occasion de travailler lors de son stage sur l’optimisation et l’amélioration d’un outil stratégique. Il a également pu concevoir et mettre en œuvre une solution d’export de données optimisée pour supporter les différentes masses données générées par les utilisateurs sous forme d’API. Sofiane fait également parti de nos Tech’Angels et prend part activement à l’animation de notre communauté Tech. ”