Java 11 HTTP Client Interview Questions and Answers

Master Java 11 HTTP Client with production-ready interview questions covering synchronous and asynchronous requests, HTTP/2, WebSocket, authentication, timeout, CompletableFuture integration, and enterprise use cases.

Introduction

Before Java 11, developers typically used:

  • HttpURLConnection
  • Apache HttpClient
  • OkHttp
  • Jersey Client
  • RestTemplate (Spring)

The built-in HttpURLConnection API was difficult to use and lacked modern features such as asynchronous requests and HTTP/2 support.

Java 11 introduced the new java.net.http.HttpClient API, which is:

  • Modern
  • Immutable
  • Thread-safe
  • HTTP/2 Ready
  • Asynchronous
  • CompletableFuture Compatible

Today, the Java 11 HTTP Client is widely used in:

  • Microservices
  • REST Clients
  • Cloud Applications
  • API Gateways
  • Third-party Integrations
  • Enterprise Systems

1. What is the Java 11 HTTP Client?

Answer

The Java 11 HTTP Client is the modern HTTP API introduced in the java.net.http package.

It replaces the older HttpURLConnection API.

Major capabilities include:

  • HTTP/1.1
  • HTTP/2
  • HTTPS
  • WebSocket
  • Synchronous requests
  • Asynchronous requests
  • SSL support
  • Redirect handling

Example

HttpClient client =
HttpClient.newHttpClient();

2. Why was the new HTTP Client introduced?

Answer

The old HttpURLConnection API had several limitations.

Problems

  • Verbose code
  • Difficult configuration
  • No HTTP/2 support
  • No asynchronous API
  • Poor readability

The new HTTP Client provides:

  • Fluent API
  • Immutable objects
  • Better performance
  • HTTP/2 support
  • CompletableFuture integration

It simplifies REST communication in enterprise applications.


3. What are the main classes of the HTTP Client API?

Answer

The API mainly consists of three classes.

HttpClient

Creates and sends requests.

HttpClient

HttpRequest

Represents an HTTP request.

HttpRequest

HttpResponse

Represents the server response.

HttpResponse

Together, these classes form the complete HTTP communication workflow.


4. How do you send a synchronous HTTP request?

Answer

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()
);

System.out.println(
response.body()
);

The calling thread waits until the response is received.


5. How do you send an asynchronous HTTP request?

Answer

Use

sendAsync()

Example

client.sendAsync(
request,
HttpResponse.BodyHandlers.ofString()
)
.thenApply(HttpResponse::body)
.thenAccept(System.out::println);

The method returns

CompletableFuture<HttpResponse<String>>

This enables non-blocking communication.


6. What is the advantage of HTTP/2 support?

Answer

Java 11 HTTP Client supports HTTP/2.

Benefits include:

  • Multiplexing
  • Header compression
  • Better throughput
  • Lower latency
  • Reduced connections

This improves performance for cloud-native applications.


7. How do you configure a timeout?

Answer

Example

HttpClient client =
HttpClient.newBuilder()
          .connectTimeout(
              Duration.ofSeconds(5)
          )
          .build();

Request timeout

HttpRequest request =
HttpRequest.newBuilder()
           .timeout(
               Duration.ofSeconds(10)
           )
           .uri(
               URI.create(url)
           )
           .GET()
           .build();

Timeouts prevent applications from waiting indefinitely.


8. How do you add HTTP headers?

Answer

Example

HttpRequest request =
HttpRequest.newBuilder()
           .uri(
               URI.create(url)
           )
           .header(
               "Authorization",
               "Bearer token"
           )
           .header(
               "Accept",
               "application/json"
           )
           .GET()
           .build();

Common headers

  • Authorization
  • Content-Type
  • Accept
  • User-Agent

9. How do you send a POST request?

Answer

Example

HttpRequest request =
HttpRequest.newBuilder()
           .uri(
               URI.create(url)
           )
           .header(
               "Content-Type",
               "application/json"
           )
           .POST(
               HttpRequest
               .BodyPublishers
               .ofString(json)
           )
           .build();

Body publishers support:

  • String
  • File
  • Byte Array
  • InputStream

10. How does the HTTP Client integrate with CompletableFuture?

Answer

The asynchronous API returns a CompletableFuture.

Example

client.sendAsync(
request,
HttpResponse.BodyHandlers.ofString()
)
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.exceptionally(ex -> {

    ex.printStackTrace();

    return null;

});

Benefits

  • Non-blocking execution
  • Callback chaining
  • Better scalability
  • Improved responsiveness

11. Explain a production use case of the Java 11 HTTP Client.

Answer

Scenario

An insurance microservice aggregates information from multiple downstream services.

Services

  • Customer Service
  • Policy Service
  • Claims Service
  • Rewards Service

Implementation

CompletableFuture<HttpResponse<String>>

Each service call executes asynchronously.

Finally,

CompletableFuture.allOf(...)

waits for all responses before building the final API response.

Result

  • Faster API responses
  • Better throughput
  • Improved scalability
  • Reduced blocking

12. What are the advantages of the Java 11 HTTP Client?

Answer

Advantages include:

  • Modern API
  • HTTP/2 support
  • Asynchronous programming
  • Immutable objects
  • Thread safety
  • CompletableFuture integration
  • Better readability
  • Built into the JDK

It eliminates the need for many third-party HTTP libraries.


13. What are common mistakes while using the HTTP Client?

Answer

Common mistakes include:

Ignoring timeouts.

Blocking immediately using

future.join();

for every asynchronous call.

Creating a new HttpClient for every request.

Ignoring exception handling.

Not reusing client instances.

A single HttpClient instance is designed to be reused.


14. What are the best practices for the Java 11 HTTP Client?

Answer

Recommended practices:

  • Reuse HttpClient instances.
  • Configure connection and request timeouts.
  • Prefer sendAsync() for independent service calls.
  • Handle exceptions properly.
  • Use HTTPS whenever possible.
  • Use HTTP/2 if supported.
  • Reuse BodyHandlers.
  • Integrate with CompletableFuture for scalable workflows.
  • Monitor request latency and failures.

These practices improve reliability and performance.


15. What interview tips should you remember about the Java 11 HTTP Client?

Answer

Interviewers commonly ask:

  • Why the new HTTP Client was introduced.
  • HttpURLConnection vs HttpClient.
  • Synchronous vs Asynchronous requests.
  • HTTP/2 support.
  • POST requests.
  • Timeouts.
  • Authentication headers.
  • CompletableFuture integration.
  • Production use cases.

Remember

  • Java 11 introduced the modern java.net.http.HttpClient.
  • It supports HTTP/1.1 and HTTP/2.
  • send() is synchronous.
  • sendAsync() is asynchronous.
  • Asynchronous requests return CompletableFuture.
  • Reuse HttpClient instances for better performance.
  • Configure timeouts and handle exceptions.
  • The HTTP Client is widely used in microservices and cloud applications.

Summary

The Java 11 HTTP Client provides a modern, efficient, and scalable way to communicate with HTTP services. With built-in HTTP/2 support, asynchronous programming, and seamless integration with CompletableFuture, it has become the preferred choice for building REST clients in enterprise Java applications.

Key Takeaways

  • Understand why the Java 11 HTTP Client was introduced.
  • Learn the roles of HttpClient, HttpRequest, and HttpResponse.
  • Know how to send synchronous and asynchronous requests.
  • Configure connection and request timeouts.
  • Add headers and send POST requests.
  • Integrate HTTP calls with CompletableFuture.
  • Take advantage of HTTP/2 features.
  • Reuse HttpClient instances for better performance.
  • Follow best practices for enterprise applications.
  • Support interview answers with real-world production examples.