Java 11 introduced a new HTTP API in java.net.http built around HttpClient, HttpRequest, and HttpResponse. The API uses an immutable builder pattern for both clients and requests, supports HTTP/2 by default, and offers both synchronous (send) and asynchronous (sendAsync) request handling. The synchronous approach blocks per request, while sendAsync returns a CompletableFuture immediately, allowing chained processing with thenApply and thenAccept. A practical example searches Wikipedia pages for a term, demonstrating that async handling typically completes in about 75% of the time of the blocking approach. Reactive request/response body handling via BodyPublisher and BodyHandler is introduced but deferred to a follow-up post.

9m read timeFrom nipafx.dev
Post cover image
Table of contents
▚ The Building Blocks▚ Synchronous HTTP Request Handling▚ Asynchronous HTTP Request Handling▚ Summary

Sort: