Use the modern Java 11 HttpClient to interact with web services and APIs.
Interacting with web services and REST APIs is a cornerstone of modern application development. The standard way to do this is by sending HTTP requests. Java 11 introduced a new, modern `HttpClient` API in the `java.net.http` package, which is designed to be more flexible, easier to use, and more performant than the legacy `HttpURLConnection` class. The new API is built around three main classes: `HttpClient`, `HttpRequest`, and `HttpResponse`. The `HttpClient` is the entry point for sending requests. You typically create one client instance and reuse it for multiple requests. The `HttpRequest` object represents the request you want to send. It is built using a fluent builder pattern, allowing you to easily specify the URI, the HTTP method (GET, POST, etc.), headers, and the request body. `HttpRequest` objects are immutable, making them safe to share. Once the request is built, you use the client's `send()` or `sendAsync()` method to send it. The `send()` method is synchronous and blocks until the response is received. The `sendAsync()` method is asynchronous and returns a `CompletableFuture`, allowing your application to perform other tasks while waiting for the response. The `HttpResponse` object contains the response from the server, including the status code, headers, and the response body. This modern API supports HTTP/1.1, HTTP/2, and WebSockets, making it the definitive choice for any HTTP communication in new Java applications.