OkHttp Proxy Setup
OkHttp is the most widely used HTTP client in the Java and Android ecosystem. Its connection pooling, transparent compression, and interceptor architecture make it an excellent choice for proxy-based workflows. OkHttp handles proxy CONNECT tunneling automatically for HTTPS targets, and its authenticator callback cleanly separates proxy credentials from request logic.
Complete Configuration with Authentication
import okhttp3.*;
import java.net.InetSocketAddress;Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("gate.hexproxies.com", 8080));
OkHttpClient client = new OkHttpClient.Builder() .proxy(proxy) .proxyAuthenticator((route, response) -> { String credential = Credentials.basic( System.getenv("PROXY_USER"), System.getenv("PROXY_PASS") ); return response.request().newBuilder() .header("Proxy-Authorization", credential) .build(); }) .connectTimeout(15, java.util.concurrent.TimeUnit.SECONDS) .readTimeout(30, java.util.concurrent.TimeUnit.SECONDS) .connectionPool(new ConnectionPool(20, 5, java.util.concurrent.TimeUnit.MINUTES)) .build();
Request request = new Request.Builder() .url("https://httpbin.org/ip") .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)") .build();
try (Response response = client.newCall(request).execute()) { System.out.println(response.body().string()); } ```
Using Interceptors for Proxy Metrics
OkHttp's interceptor chain lets you log proxy performance without modifying request code. Add a network interceptor to track response times, status codes, and data transfer through the proxy:
client = client.newBuilder()
.addNetworkInterceptor(chain -> {
long start = System.nanoTime();
Response response = chain.proceed(chain.request());
long elapsed = (System.nanoTime() - start) / 1_000_000;
System.out.printf("[%d] %s - %dms%n",
response.code(), chain.request().url(), elapsed);
return response;
})
.build();Common Pitfalls
The most frequent mistake is creating a new OkHttpClient per request. OkHttp maintains a connection pool and thread pool internally — creating multiple clients wastes these resources and prevents connection reuse through the proxy. Always create one client and reuse it across all requests.
Another issue: the proxyAuthenticator callback fires on every 407 response. If your credentials are wrong, OkHttp will retry indefinitely. Add a retry counter in the authenticator to bail after 3 attempts and prevent infinite authentication loops.
Retry and Rotation Strategy
For residential proxy rotation, generate a unique session ID per request and embed it in the proxy username. Create a new proxy authenticator that appends the session ID:
String sessionUser = System.getenv("PROXY_USER") + "-session-" +
UUID.randomUUID().toString().substring(0, 8);Performance Tuning
Set the connection pool size to match your proxy plan's concurrency limit. OkHttp defaults to 5 idle connections — increase this for high-throughput proxy workloads. Set connect timeout to 15 seconds for residential proxies, which have higher handshake latency than direct connections.