Code and command line
Java proxy setup
Java has a built-in HTTP client since version 11 with proper proxy support, but one default catches almost everyone. The JDK refuses to send Basic credentials when it opens an HTTPS tunnel through a proxy. Requests to https pages then fail with a 407 even though the login is correct.
The fix is a single system property. This guide shows where to put it, how to wire the proxy and the Authenticator, and an OkHttp version that does not depend on that property at all.
What do you need before adding a proxy in Java?
Tick these off before the first step. Your login and password appear in your account once the first top-up of $40 or more has been credited.
| Requirement | Detail |
|---|---|
| JDK 11 or newer | For java.net.http.HttpClient. Any current LTS release works. |
| The tunnelling property | jdk.http.auth.tunneling.disabledSchemes set to an empty value. |
| TopProxy login and password | Login such as tp7k2m9_country-us. |
| OkHttp, optional | com.squareup.okhttp3:okhttp for the OkHttp step. |
Where do the proxy host, port and login go in Java?
Work through the steps in order. Swap YOUR_PASSWORD for your proxy password and tp7k2m9 for your account name.
Allow Basic auth for HTTPS tunnels
The JDK lists Basic in
jdk.http.auth.tunneling.disabledSchemesby default, which blocks the login on CONNECT. Clear the list. A JVM flag is the most reliable place because it is read before any networking class loads.java -Djdk.http.auth.tunneling.disabledSchemes= -jar app.jarTip.If you set it in code, do it before creating any HttpClient or opening a URL connection, otherwise the old value may already be cached.
Create an HttpClient with proxy and Authenticator
The ProxySelector points every request at the gateway. The Authenticator returns the login only when the request comes from the proxy, so it never leaks to a website that asks for credentials.
import java.net.*; import java.net.http.*; public class ProxyCheck { public static void main(String[] args) throws Exception { System.setProperty("jdk.http.auth.tunneling.disabledSchemes", ""); String login = "tp7k2m9_country-us"; String password = System.getenv("TOPPROXY_PASSWORD"); HttpClient client = HttpClient.newBuilder() .proxy(ProxySelector.of(new InetSocketAddress("proxy.topproxy.shop", 8080))) .authenticator(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { if (getRequestorType() == RequestorType.PROXY) { return new PasswordAuthentication(login, password.toCharArray()); } return null; } }) .build(); HttpRequest req = HttpRequest.newBuilder(URI.create("https://ipinfo.io/json")).build(); HttpResponse<String> res = client.send(req, HttpResponse.BodyHandlers.ofString()); System.out.println(res.statusCode() + " " + res.body()); } }Or use OkHttp with a proxy authenticator
OkHttp answers the proxy challenge itself through
proxyAuthenticator, so the JDK property does not apply. It is a good choice on Android and in projects that already use OkHttp.OkHttpClient client = new OkHttpClient.Builder() .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.topproxy.shop", 8080))) .proxyAuthenticator((route, response) -> { String cred = Credentials.basic("tp7k2m9_country-us", System.getenv("TOPPROXY_PASSWORD")); return response.request().newBuilder().header("Proxy-Authorization", cred).build(); }) .build(); try (Response r = client.newCall(new Request.Builder().url("https://ipinfo.io/json").build()).execute()) { System.out.println(r.body().string()); }
How can you confirm the proxy is active in Java?
Run ProxyCheck. A 200 with a JSON body that shows a foreign address means the tunnel and the login both work. A 407 almost always means the tunnelling property was not applied.
export TOPPROXY_PASSWORD="your-password"
java -Djdk.http.auth.tunneling.disabledSchemes= ProxyCheck.javaThe response shown here is shortened to the fields that matter. The region and time zone follow the exit, in this case India.
{
"ip": "198.51.100.47",
"city": "Bengaluru",
"region": "Karnataka",
"country": "IN",
"timezone": "Asia/Kolkata"
}198.51.100.47 is an address reserved for documentation. A live check prints a real exit.
How do you switch between rotating and held IPs in Java?
HttpClient pools connections, so requests can share an exit while a pooled connection lives. Build a new client when you need a new address on a rotating login. For a fixed address through a whole job, use a session login.
String login = "tp7k2m9_country-us";How the login is put together
Every setting lives in the login, joined with underscores. Nothing else in Java changes when you switch location or session.
| Part | Meaning | Rule |
|---|---|---|
| tp7k2m9 | Your account name | Always first. Yours is shown in your account and replaces tp7k2m9 in every example here. |
| _country-us | Exit country | Two-letter code in lowercase. 64 countries to choose from. |
| _city-new-york | Exit city, optional | Lowercase, spaces become hyphens. 172 cities in total. |
| _session-a1b2c3 | Session name, optional | Letters and digits you pick. Same name, same exit. New name, new exit. |
| _life-30 | Session life in minutes | Only with a session. Residential 1 to 120 minutes, mobile 1 to 60 minutes, long session ISP up to 12 hours (life 720). |
All parts togethertp7k2m9_country-us_city-new-york_session-a1b2c3_life-30
How do you choose an exit country or city in Java?
Put the country and city into the login string. One client per login keeps the setup simple.
String login = "tp7k2m9_country-in";Codes and city values for all 64 countries are listed on the proxy locations pages.
How do you fix a 407 or connection error in Java?
Find the line that matches what you see. Most problems come down to the login, the port or the network you are on.
| Symptom | Likely cause | Fix |
|---|---|---|
407 on every https URL, http URLs fine | Basic is still disabled for tunnels. | Pass -Djdk.http.auth.tunneling.disabledSchemes= on the command line. |
IOException: too many authentication attempts | The Authenticator keeps returning a refused login. | Check the login and password, and that the environment variable is visible to the JVM. |
| NullPointerException in the Authenticator | TOPPROXY_PASSWORD is not set, so getenv returned null. | Set the variable or read the password from your config system. |
| SOCKS proxy ignored by HttpClient | java.net.http does not support SOCKS proxies. | Use the HTTP port 8080, or HttpURLConnection with a SOCKS Proxy object. |
Still stuck? Run the same login through the cURL check. If cURL connects, the issue is in the Java settings. If it does not, write to support with the error text.
What kind of proxy pairs well with Java?
All products sit behind the same gateway and share one prepaid balance, so moving from one to another means buying the other product and changing the login, nothing more.
- Rotating Residential ProxiesHousehold exits for Java crawlers and price monitors.$0.23 per GB
- Dedicated ISP ProxiesFixed fast addresses for backend services that call partner APIs from an allowlisted IP.$0.33 per IP per month
- Datacenter Proxy PoolLarge IP packs at a low price for bulk lookups.$0.58 per 100 IPs per month
Calling pages from code at scale? The TopProxy API docs cover the Scraping API, which renders pages and retries on its own.
What else do Java users ask about proxies?
Why does Java disable Basic auth for tunnels?
It is a conservative default added years ago, because Basic credentials are only encoded, not encrypted, on the hop to the proxy. Clearing the property restores the usual behaviour that other HTTP clients have.
Can I use the http.proxyHost system properties instead?
Yes for HttpURLConnection: set https.proxyHost and https.proxyPort, plus a default Authenticator. HttpClient ignores those properties unless you pass ProxySelector.getDefault().
Does this work with Spring WebClient or RestTemplate?
Both sit on top of an HTTP library such as Reactor Netty, Apache HttpClient or the JDK client. Configure the proxy on that underlying client with the same host, port and login.
How do I use the SOCKS5 port from Java?
With HttpURLConnection and a Proxy of type SOCKS pointed at port 1080, plus a default Authenticator for the login. HttpClient itself does not speak SOCKS, so the HTTP port is the simpler route.
Is the login cached between requests?
Yes. Once the Authenticator has answered, HttpClient reuses the credentials for later requests to the same proxy, so the callback is not called every time.
Related setup guides
- Gohttp.Transport with http.ProxyURL and url.UserPassword, SOCKS5 through the same field, and goroutine-safe clients.
- PHPThe cURL extension with CURLOPT_PROXY and CURLOPT_PROXYUSERPWD, SOCKS5 hostname mode, and Guzzle.
- Node.jsfetch with an undici ProxyAgent, axios with https-proxy-agent, and SOCKS5 through socks-proxy-agent.
- AndroidA manual Wi-Fi proxy with no password field, the browser sign-in that follows, emulator flags, and app options.
Ready to connect Java?
Open an account, add $40 or more in crypto, and your login and password are waiting in the account area.