Apache HttpClient Proxy Setup
Apache HttpClient (part of Apache HttpComponents) is the enterprise standard HTTP client for Java applications. It provides fine-grained control over connection management, proxy routing, and authentication through its builder pattern. The client's connection pooling works efficiently through proxy gateways, making it well-suited for high-throughput proxy workloads.
Complete Configuration with Authentication
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;HttpHost proxy = new HttpHost("gate.hexproxies.com", 8080);
CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(proxy), new UsernamePasswordCredentials( System.getenv("PROXY_USER"), System.getenv("PROXY_PASS") ) );
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setMaxTotal(50); connManager.setDefaultMaxPerRoute(20);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CloseableHttpClient client = HttpClients.custom() .setRoutePlanner(routePlanner) .setDefaultCredentialsProvider(credsProvider) .setConnectionManager(connManager) .build();
HttpGet request = new HttpGet("https://httpbin.org/ip"); request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
try (CloseableHttpResponse response = client.execute(request)) { String body = EntityUtils.toString(response.getEntity()); System.out.printf("[%d] %s%n", response.getStatusLine().getStatusCode(), body); } ```
Connection Pool Configuration
Apache HttpClient's PoolingHttpClientConnectionManager maintains persistent connections to the proxy gateway. Configure `maxTotal` to match your proxy plan's concurrency limit and `defaultMaxPerRoute` to control connections per target host. The connection pool reuses TCP connections through the proxy, avoiding the overhead of repeated CONNECT handshakes.
Common Pitfalls
The most common mistake is not closing the HttpClient or response entities. Unclosed responses leak connections from the pool, eventually exhausting available connections through the proxy. Always use try-with-resources for both the client and response objects.
Another issue: Apache HttpClient defaults to preemptive proxy authentication only when using specific auth schemes. If you see 407 responses, add a preemptive auth interceptor to send credentials on the first request rather than waiting for the challenge-response cycle.
Retry and Rotation
For IP rotation, generate a unique session ID per request and embed it in the proxy credentials:
String sessionUser = System.getenv("PROXY_USER")
+ "-session-" + UUID.randomUUID().toString().substring(0, 8);
credsProvider.setCredentials(
new AuthScope(proxy),
new UsernamePasswordCredentials(sessionUser,
System.getenv("PROXY_PASS"))
);Timeout Configuration
Set connect timeout to 15 seconds and socket timeout to 30 seconds for residential proxy connections. The proxy CONNECT handshake adds latency before the target connection is established.