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.