Sub topic 18
Proxy, cookies, TLS and custom executors
Assume Java 11 or later, basic HTTP knowledge, and familiarity with checked exceptions. This chapter focuses on client-wide settings that affect how HttpClient connects and stores state.
Configure a proxy and cookies
A proxy is an intermediate server through which the client sends requests. Configure one with proxy. A CookieManager stores cookies received from servers and sends them on later requests.
| Setting | Builder method | Purpose |
|---|---|---|
| Proxy | `proxy(ProxySelector)` | Routes connections through a proxy |
| Cookies | `cookieHandler(CookieHandler)` | Stores and supplies HTTP cookies |
| Executor | `executor(Executor)` | Runs asynchronous client work |
A client can be shared safely by multiple requests. A shared cookie manager also means that those requests share its cookie state.
import java.io.IOException;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.HttpClient;
import java.net.HttpRequest;
import java.net.HttpResponse;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.net.ssl.SSLContext;
public class ConfiguredClient {
public static void main(String[] args)
throws IOException, InterruptedException, NoSuchAlgorithmException {
ExecutorService executor = Executors.newFixedThreadPool(4);
CookieManager cookies =
new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER);
HttpClient client = HttpClient.newBuilder()
.proxy(ProxySelector.of(
new InetSocketAddress("proxy.example.com", 8080)))
.cookieHandler(cookies)
.sslContext(SSLContext.getDefault())
.executor(executor)
.build();
HttpRequest request = HttpRequest.newBuilder(
URI.create("https://example.com/"))
.GET()
.build();
try {
HttpResponse<String> response =
client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
} finally {
executor.shutdown();
}
}
}The proxy address must be replaced with a real proxy. CookiePolicy.ACCEPT_ORIGINAL_SERVER accepts cookies from the server that set them. Avoid accepting every cookie unless you understand the security and privacy consequences.
Configure TLS
TLS protects HTTPS connections. By default, the client uses the Java runtime's default SSLContext, which uses the runtime trust store to verify server certificates.
Use a custom SSLContext when an organization requires a private certificate authority or when the client must present a client certificate. Build it from a carefully configured KeyStore containing trusted certificates or private keys.
Do not install a trust manager that accepts every certificate. That disables server identity checks and makes HTTPS vulnerable to interception. Keep certificate and key material outside source code, protect private keys, and rotate them as required.
Choose an executor
An executor controls threads used by asynchronous operations. A bounded executor, such as a fixed thread pool, prevents unlimited thread creation. The application owns this executor and should shut it down when the client is no longer needed.
Exercise
Why should an application reuse one configured HttpClient instead of creating a new client for every request?
Show answer
Reuse preserves connection pooling and shared configuration, such as proxy, cookies, TLS settings, and the executor.