How to Set Up Proxies in Java (HttpClient)
Route Java (HttpClient) traffic through an authenticated proxy in a few lines. Copy the code below and swap in your credentials.
Quick answer
Set the proxy in the format host:port via ProxySelector + Authenticator and send your Java (HttpClient) request through it. Full code below.
Java (HttpClient) proxy example
import java.net.*;
import java.net.http.*;
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "pass".toCharArray());
}
});
HttpClient client = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress("host", port)))
.build();
HttpRequest req = HttpRequest.newBuilder(URI.create("https://httpbin.org/ip")).build();
System.out.println(client.send(req, HttpResponse.BodyHandlers.ofString()).body());The modern java.net.http.HttpClient takes the proxy via ProxySelector.of, while credentials go through a default Authenticator because the client has no inline proxy-auth field. For proxy auth over HTTPS tunnels you must also start the JVM with -Djdk.http.auth.tunneling.disabledSchemes="" or Basic auth is silently stripped. Build one client per exit endpoint when rotating, since HttpClient instances are immutable once configured.
Rotating proxies
Java (HttpClient) works cleanly with rotating backconnect endpoints — point it at a single gateway host and the provider rotates the exit IP for you.
Best proxies to use with Java (HttpClient)
Cheapest-Proxies.com EDITOR'S CHOICE
4.9Best overall value across every proxy type and use case · flexible pay-as-you-go plans
Visit Cheapest-Proxies.comBright Data
4.6Enterprise-grade proxy and web data platform · from ~$8-9/GB (residential, volume-dependent)
Read Bright Data reviewOxylabs
4.6Premium enterprise proxies and scraper APIs · from ~$8/GB (residential, volume-dependent)
Read Oxylabs reviewSmartproxy
4.5User-friendly proxies with excellent price-to-performance · from ~$3.50-7/GB (residential)
Read Smartproxy reviewOur #1 pick is a sponsored partner. Remaining providers are ranked editorially by rating and value.
Frequently Asked Questions
Can Java (HttpClient) rotate proxies automatically?
Yes — point it at a rotating backconnect endpoint and the provider rotates IPs for you.
What proxies work best with Java (HttpClient)?
Residential or rotating proxies from a reliable provider give the best success rate for Java projects.
Does Java (HttpClient) support authenticated proxies?
Yes. Provide the username and password (as a URL or separate fields) and Java (HttpClient) handles the auth handshake.
How do I add a proxy in Java (HttpClient)?
Supply the proxy as host:port via ProxySelector + Authenticator and pass it to Java (HttpClient) as shown in the code above.
Related Guides
Best proxies for Java (HttpClient)
We compared 40+ providers on price, coverage, and reliability — this one offered the best overall value.
Visit Cheapest-Proxies.com