Java 11 HTTP Client API: Unleashing the Power of Modern Web Communications

Naveen Metta
5 min readJul 8, 2023

Introduction:
In the ever-evolving world of web development, effective communication between applications is crucial. Java, being one of the most widely used programming languages, has constantly evolved its networking capabilities. With the release of Java 11, a new and improved HTTP Client API was introduced, revolutionizing the way Java developers interact with HTTP-based services. In this article, we will delve into the features, benefits, and practical usage of the Java 11 HTTP Client API.

  1. The Evolution of HTTP Communication in Java:
    In previous versions of Java, the most commonly used libraries for HTTP communication were Apache HttpClient and the legacy HttpURLConnection class. While these libraries served their purpose, they had limitations. The APIs were often complex, lacking flexibility, and struggled to keep up with modern web standards. Recognizing these limitations, Java 11 addressed these shortcomings with the introduction of the HTTP Client API.
  2. Enter Java 11 HTTP Client API:
    Java 11 brought a modernized and streamlined HTTP Client API, providing developers with a clean and efficient way to interact with HTTP-based services. This new API is part of the java.net.http package and is built upon the principles of the Reactive Streams API, making it asynchronous and non-blocking by default. This means that applications can send HTTP requests and continue their execution without waiting for the responses, leading to improved performance and responsiveness.
  3. Key Features and Benefits:
    3.1 Asynchronous and Non-Blocking Communication:
    One of the standout features of the Java 11 HTTP Client API is its support for asynchronous and non-blocking communication. This allows developers to send multiple requests concurrently, improving performance and scalability in applications. Asynchronous communication is particularly useful when dealing with long-running requests or when parallelizing multiple independent requests. The API leverages the CompletableFuture framework to handle asynchronous operations, making it easier to write efficient and responsive code.

Example:

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/data"))
.build();

CompletableFuture<HttpResponse<String>> responseFuture =
client.sendAsync(request, HttpResponse.BodyHandlers.ofString());

responseFuture.thenAccept(response ->
System.out.println("Response code: " + response.statusCode()));

3.2 Fluent and Expressive API: The API design of the Java 11 HTTP Client API follows a fluent and expressive style, making it easy to read and write HTTP requests and responses. The Builder pattern is extensively used, allowing developers to construct requests with a chain of method calls, enhancing code clarity and maintainability. This approach simplifies setting headers, handling request bodies, and configuring timeouts and redirects. It also promotes a declarative coding style, enabling developers to express their intent clearly.

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/data"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"key\": \"value\"}"))
.build();

3.3 HTTP/2 Support:
The Java 11 HTTP Client API fully supports the HTTP/2 protocol, offering benefits such as multiplexing, server push, and improved performance over HTTP/1.1. The API automatically negotiates the protocol with the server, providing a seamless transition for developers. By leveraging the features of HTTP/2, applications can achieve higher throughput, reduced latency, and better resource utilization.

HTTP/2 improves the efficiency of data transfer by multiplexing multiple streams over a single connection, eliminating the need for multiple connections. It also introduces server push, where the server can proactively send additional resources to the client before they are requested. These features contribute to a more efficient and responsive web communication experience.

3.4 Authentication and Cookies:
The Java 11 HTTP Client API provides built-in support for various authentication mechanisms, including Basic, Digest, and Bearer token authentication. Developers can easily include authentication headers in their requests, ensuring secure communication with web services. Additionally, the API handles cookies transparently, simplifying the management of session-based interactions with web services. This enables seamless authentication and session management in applications.

Practical Usage and Real-World Scenarios:
4.1 RESTful API Consumption:
The Java 11 HTTP Client API simplifies the consumption of RESTful APIs. Developers can easily send GET, POST, PUT, DELETE, and other HTTP methods, handle JSON responses using libraries like Jackson or Gson, and seamlessly integrate with existing frameworks like Spring or JAX-RS. The API also supports content negotiation, allowing developers to specify the desired response format using headers like “Accept”.

Example:

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/users/1"))
.header("Accept", "application/json")
.GET()
.build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Response body: " + response.body());

4.2 Web Scraping and Data Extraction:
The Java 11 HTTP Client API streamlines web scraping and data extraction tasks. By sending HTTP requests to web pages and parsing the HTML responses using libraries like Jsoup, developers can extract structured data for various purposes, such as content aggregation, data analysis, or even building custom search engines. The API allows for easy retrieval of HTML content, making it a valuable tool for scraping websites and extracting relevant information.

Example

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://example.com"))
.GET()
.build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
String html = response.body();

// Parse HTML using Jsoup
Document doc = Jsoup.parse(html);
Elements links = doc.select("a[href]");

for (Element link : links) {
System.out.println("Link: " + link.attr("href"));
}
  1. Load Balancing and Connection Pooling: The Java 11 HTTP Client API includes built-in support for load balancing and connection pooling, making it suitable for distributed systems and high-traffic applications. It allows developers to create and manage connection pools, enabling efficient reuse of connections and reducing the overhead of establishing new connections for each request. Additionally, the API supports connection pooling configurations to optimize resource usage and improve performance.

Example:

HttpClient client = HttpClient.newBuilder()
.connectionPool(ConnectionPool.newBuilder()
.maxConnections(50)
.build())
.build();
  1. Handling Redirects: The Java 11 HTTP Client API simplifies the handling of redirects in HTTP requests. By default, the API automatically follows redirects, allowing developers to easily handle scenarios where the requested resource has been moved or requires authentication. The API also provides options to control the behavior of redirects, such as maximum follow limit and redirect policy customization.

Example:

HttpClient client = HttpClient.newBuilder()
.followRedirects(Redirect.NORMAL)
.build();
  1. Proxy Support: The Java 11 HTTP Client API supports working with proxies, enabling applications to route their HTTP requests through a proxy server. Developers can configure the API to use a specific proxy server, provide authentication credentials if required, and handle scenarios where multiple proxy servers are available.

Example:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080));

HttpClient client = HttpClient.newBuilder()
.proxy(proxy)
.build();
  1. Timeout Configuration: The Java 11 HTTP Client API allows developers to configure timeouts for their HTTP requests. This is particularly useful when dealing with scenarios where a response is expected within a certain time frame. By setting appropriate timeout values, applications can avoid waiting indefinitely for a response and gracefully handle timeout scenarios.

Example:

HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
  1. SSL/TLS Configuration: The Java 11 HTTP Client API provides extensive support for SSL/TLS configurations, ensuring secure communication over HTTPS. Developers can customize SSL/TLS parameters, specify trust stores, and handle scenarios where client certificates are required for authentication. This allows applications to securely communicate with HTTPS-based services.

Example:

HttpClient client = HttpClient.newBuilder()
.sslContext(SSLContext.getDefault())
.build();

Conclusion: The Java 11 HTTP Client API brings a modern and efficient approach to web communication in Java. With its asynchronous and non-blocking nature, support for HTTP/2, authentication mechanisms, load balancing, connection pooling, and a fluent API design, it simplifies the development of robust and scalable applications. Whether you’re consuming RESTful APIs, performing web scraping, managing proxies, or configuring SSL/TLS, the Java 11 HTTP Client API empowers you with the tools you need to succeed in the dynamic world of web development. Embrace its power, and unlock a new level of connectivity in your Java applications.

--

--

Naveen Metta

I'm a Full Stack Developer with 2.5 years of experience. feel free to reach out for any help : mettanaveen701@gmail.com