v1.10.82-f67ee7d
Skip to main content
← Back to Hex Proxies

Apache HttpClient (Java) Proxy Integration

Use Hex Proxies with Apache HttpClient for proxy-enabled Java requests.

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.

Integration Steps

1

Create proxy host and credentials provider

Initialize HttpHost with gate.hexproxies.com:8080 and configure a BasicCredentialsProvider with your proxy username and password from environment variables.

2

Configure connection pool

Create a PoolingHttpClientConnectionManager with maxTotal and defaultMaxPerRoute matching your proxy plan concurrency limits.

3

Build the client with proxy route planner

Use HttpClients.custom() to attach the route planner, credentials provider, and connection manager. Reuse the client across all requests.

4

Execute requests and handle responses

Use try-with-resources for response handling to prevent connection leaks. Check status codes for proxy blocks (403, 429) and implement retry logic.

Operational Tips

Keep sessions stable for workflows that depend on consistent identity. For high-volume collection, rotate IPs and reduce concurrency if you see timeouts or 403 responses.

  • Prefer sticky sessions for multi-step flows (auth, checkout, forms).
  • Rotate per request for scale and broad coverage.
  • Use timeouts and retries to handle transient failures.

Frequently Asked Questions

How do I add proxy authentication in Apache HttpClient?

Create a BasicCredentialsProvider, set credentials with AuthScope matching the proxy host, and pass the provider to HttpClients.custom().setDefaultCredentialsProvider(). This handles the 407 Proxy Authentication Required challenge automatically.

Why do I get connection pool exhaustion errors?

Unclosed response entities hold connections from the pool. Always call EntityUtils.consume() or close the response in a try-with-resources block. Also ensure maxTotal is set high enough for your concurrency requirements.

How do I rotate IPs with Apache HttpClient?

Embed a unique session ID in the proxy username per request. Update the CredentialsProvider before each request, or create request-scoped contexts with different credentials for each session.

Ready to Integrate?

Start using residential proxies with Apache HttpClient (Java) today.