Apache HttpComponents Client fluent API providing a simplified interface for HTTP operations
—
The Executor class manages authentication credentials, cookies, and connection pooling for multiple HTTP requests. It maintains a shared context that persists across request executions.
Create Executor instances using static factory methods:
public static Executor newInstance();
public static Executor newInstance(HttpClient httpclient);import org.apache.http.client.fluent.Executor;
import org.apache.http.impl.client.HttpClients;
// Default executor with built-in HttpClient
Executor executor = Executor.newInstance();
// Custom executor with your own HttpClient
HttpClient customClient = HttpClients.createDefault();
Executor customExecutor = Executor.newInstance(customClient);Configure various authentication schemes using fluent methods:
public Executor use(CredentialsProvider credentialsProvider);
public Executor auth(AuthScope authScope, Credentials creds);
public Executor auth(HttpHost host, Credentials creds);
public Executor auth(String host, Credentials creds);
public Executor auth(Credentials cred);
public Executor auth(String username, String password);
public Executor auth(String username, String password, String workstation, String domain);
public Executor auth(HttpHost host, String username, String password);
public Executor auth(HttpHost host, String username, String password, String workstation, String domain);
public Executor clearAuth();import org.apache.http.HttpHost;
// Global authentication (applies to any host)
Executor executor = Executor.newInstance()
.auth("username", "password");
// Host-specific authentication
executor = Executor.newInstance()
.auth(new HttpHost("api.example.com", 443, "https"), "user", "pass")
.auth("api2.example.com:8080", "user2", "pass2");
// Clear all authentication
executor.clearAuth();// NTLM authentication with domain and workstation
Executor executor = Executor.newInstance()
.auth("username", "password", "WORKSTATION", "DOMAIN");
// Host-specific NTLM authentication
executor = Executor.newInstance()
.auth(new HttpHost("intranet.company.com"), "user", "pass", "WS01", "COMPANY");import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
new AuthScope("api.example.com", 443),
new UsernamePasswordCredentials("username", "password")
);
Executor executor = Executor.newInstance()
.use(credentialsProvider);import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
// Authentication for specific host, port, and realm
AuthScope scope = new AuthScope("secure.example.com", 443, "admin-realm");
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "secret");
Executor executor = Executor.newInstance()
.auth(scope, creds);Enable preemptive authentication to send credentials with the first request:
public Executor authPreemptive(HttpHost host);
public Executor authPreemptive(String host);
public Executor authPreemptiveProxy(HttpHost proxy);
public Executor authPreemptiveProxy(String proxy);// Preemptive authentication for host
Executor executor = Executor.newInstance()
.auth("api.example.com", "username", "password")
.authPreemptive("api.example.com");
// Preemptive authentication for proxy
executor = Executor.newInstance()
.auth("proxy.company.com:8080", "proxyuser", "proxypass")
.authPreemptiveProxy("proxy.company.com:8080");Manage cookies across multiple requests:
public Executor use(CookieStore cookieStore);
public Executor clearCookies();import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.BasicCookieStore;
// Custom cookie store
CookieStore cookieStore = new BasicCookieStore();
Executor executor = Executor.newInstance()
.use(cookieStore);
// Execute requests that set/use cookies
executor.execute(Request.Post("https://example.com/login")
.bodyForm(Form.form()
.add("username", "user")
.add("password", "pass")
.build()));
// Subsequent requests will include session cookies
Content response = executor.execute(Request.Get("https://example.com/profile"))
.returnContent();
// Clear all cookies
executor.clearCookies();Execute requests using the configured executor context:
public Response execute(Request request) throws ClientProtocolException, IOException;import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
Executor executor = Executor.newInstance()
.auth("api.example.com", "username", "password");
// All requests executed through this executor will use the configured authentication
Response response1 = executor.execute(Request.Get("https://api.example.com/data"));
Response response2 = executor.execute(Request.Post("https://api.example.com/create")
.bodyString("{\"name\":\"test\"}", ContentType.APPLICATION_JSON));
String data1 = response1.returnContent().asString();
String data2 = response2.returnContent().asString();The Executor uses a shared connection pool with the following configuration:
public static void closeIdleConnections();// Close idle connections to free resources
Executor.closeIdleConnections();
// Typically called during application shutdown
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
Executor.closeIdleConnections();
}));@Deprecated public Executor cookieStore(CookieStore cookieStore);
@Deprecated public static void registerScheme(org.apache.http.conn.scheme.Scheme scheme);
@Deprecated public static void unregisterScheme(String name);Use use(CookieStore) instead of cookieStore(). The scheme registration methods have no effect.
The Executor class is thread-safe and can be shared across multiple threads:
// Thread-safe: can be used concurrently
Executor sharedExecutor = Executor.newInstance()
.auth("api.example.com", "username", "password");
// Multiple threads can use the same executor
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
try {
return sharedExecutor.execute(Request.Get("https://api.example.com/data1"))
.returnContent().asString();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
try {
return sharedExecutor.execute(Request.Get("https://api.example.com/data2"))
.returnContent().asString();
} catch (IOException e) {
throw new RuntimeException(e);
}
});import org.apache.http.client.fluent.*;
import java.io.IOException;
public class AuthenticatedSession {
public static void main(String[] args) {
try {
// Create executor with authentication
Executor executor = Executor.newInstance()
.auth("api.example.com", "username", "password")
.authPreemptive("api.example.com");
// Login request (sets session cookies)
String loginResponse = executor.execute(
Request.Post("https://api.example.com/login")
.bodyForm(Form.form()
.add("username", "user")
.add("password", "pass")
.build())
).returnContent().asString();
// Subsequent authenticated requests
String userData = executor.execute(
Request.Get("https://api.example.com/user/profile")
).returnContent().asString();
String updateResponse = executor.execute(
Request.Put("https://api.example.com/user/profile")
.bodyString("{\"email\":\"new@example.com\"}", ContentType.APPLICATION_JSON)
).returnContent().asString();
System.out.println("Login: " + loginResponse);
System.out.println("Profile: " + userData);
System.out.println("Update: " + updateResponse);
} catch (IOException e) {
System.err.println("Request failed: " + e.getMessage());
} finally {
// Clean up connections
Executor.closeIdleConnections();
}
}
}Install with Tessl CLI
npx tessl i tessl/maven-org-apache-httpcomponents--fluent-hc