Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407"
Basic is still disabled for tunnels. Pass -Djdk.http.auth.tunneling.disabledSchemes= on the command line, because setting it later in code can be too late once HTTP classes are loaded.
Java runs many enterprise crawlers, testing tools and Android backends. The java.net.http.HttpClient added in Java 11 supports proxies through a ProxySelector and credentials through an Authenticator, with one catch: Basic authentication for HTTPS tunnels is turned off by default in the JDK.
This guide shows the JDK property that turns it back on, a working HttpClient setup, OkHttp, which handles authentication on its own, and SOCKS5 with HttpURLConnection.
Updated 23 November 2026
HttpClient supports HTTP proxies on port 8000. For SOCKS5 on port 9000, use HttpURLConnection or OkHttp. Your username and token appear on the Proxy setup page of your dashboard after your first order.
Follow the steps in order. Replace YOUR_TOKEN with the token from your dashboard and change the country, city or session in the username to suit the job.
Since JDK 8u111 the property jdk.http.auth.tunneling.disabledSchemes defaults to Basic, which blocks username and password authentication on CONNECT tunnels. Without this change every HTTPS request through an authenticated proxy fails with 407. Set it to an empty value when you start the JVM.
java -Djdk.http.auth.tunneling.disabledSchemes= -jar app.jarPoint a ProxySelector at the gateway and answer proxy authentication requests with your username and token. The check on the requestor type keeps the credentials from being sent to target sites.
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class Main {
public static void main(String[] args) throws Exception {
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
HttpClient client = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress("gate.firstproxy.shop", 8000)))
.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType() != RequestorType.PROXY) return null;
return new PasswordAuthentication("user-country-gb", "YOUR_TOKEN".toCharArray());
}
})
.connectTimeout(Duration.ofSeconds(10))
.build();
HttpRequest request = HttpRequest.newBuilder(URI.create("https://ipinfo.io/json"))
.timeout(Duration.ofSeconds(30))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
}
}The username is a plain string, so build it per client. One HttpClient per session keeps its connections, and its IP, separate from other sessions.
static String proxyUser(String country, String city, String session, int ttl) {
StringBuilder u = new StringBuilder("user-country-").append(country);
if (city != null) u.append("-city-").append(city);
if (session != null) u.append("-session-").append(session).append("-ttl-").append(ttl).append("m");
return u.toString();
}
String user = proxyUser("gb", "manchester", java.util.UUID.randomUUID().toString().substring(0, 8), 30);OkHttp builds its own CONNECT requests, so the JDK property is not needed. Its proxyAuthenticator is called when the gateway answers 407 and returns the request with credentials attached.
// implementation("com.squareup.okhttp3:okhttp:4.12.0")
import java.net.InetSocketAddress;
import java.net.Proxy;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
OkHttpClient client = new OkHttpClient.Builder()
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("gate.firstproxy.shop", 8000)))
.proxyAuthenticator((route, response) -> response.request().newBuilder()
.header("Proxy-Authorization", Credentials.basic("user-country-gb", "YOUR_TOKEN"))
.build())
.build();
try (Response res = client.newCall(new Request.Builder().url("https://ipinfo.io/json").build()).execute()) {
System.out.println(res.code() + " " + res.body().string());
}HttpClient does not support SOCKS proxies. HttpURLConnection does, and the JDK asks the default Authenticator for the SOCKS5 username and password.
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user-country-gb", "YOUR_TOKEN".toCharArray());
}
});
Proxy socks = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("gate.firstproxy.shop", 9000));
HttpURLConnection conn = (HttpURLConnection) URI.create("https://ipinfo.io/json").toURL().openConnection(socks);
conn.setConnectTimeout(10000);
conn.setReadTimeout(30000);
System.out.println(new String(conn.getInputStream().readAllBytes()));Run the HttpClient example and read the printed JSON. A 200 with the expected country means the proxy, the property and the Authenticator are all set correctly.
A working proxy returns a response like the one shown, trimmed to the main fields. The IP belongs to the exit, the city and country follow your username, and the time zone is where that IP is located, here United Kingdom.
java -Djdk.http.auth.tunneling.disabledSchemes= Main.java{
"ip": "203.0.113.24",
"city": "London",
"country": "GB",
"timezone": "Europe/London"
}203.0.113.24 is a documentation address. Your result shows a real exit IP.
All targeting is written into the username, so Java needs no extra settings to change location. Parts are joined with hyphens in this order after your proxy user. The password is always your token.
| Part | Format | Example | Rule |
|---|---|---|---|
| Country | -country-{cc} | -country-gb | Required. Two-letter country code in lowercase. 64 countries are available. |
| City | -city-{city} | -city-london | Optional. City name in lowercase, spaces become hyphens. 172 cities are available. |
| Session | -session-{id} | -session-a1b2c3 | Optional. Any letters and digits you choose. The same id keeps the same IP, a new id gets a new IP. |
| Session length | -ttl-{minutes}m | -ttl-30m | Used with a session. Residential 1 to 120 minutes, mobile 1 to 60, long-acting ISP up to 720 (12 hours). |
user-country-gb-city-london-session-a1b2c3-ttl-30mFind country codes and city values on the proxy locations page.
The problems people run into most often with Java, with the message you are likely to see and how to solve it.
Basic is still disabled for tunnels. Pass -Djdk.http.auth.tunneling.disabledSchemes= on the command line, because setting it later in code can be too late once HTTP classes are loaded.
For HttpClient, attach it with .authenticator() on the builder. For HttpURLConnection, use Authenticator.setDefault. Also check that the method returns credentials for the PROXY requestor type.
Set connectTimeout on the client and timeout on each request. Without them a slow exit can block a thread for a long time.
Clients reuse connections, so threads that share one client also share tunnels and IPs. Create one client per session name when each thread needs its own IP.
java.net.http.HttpClient only supports HTTP proxies. Use the HTTP port with HttpClient, or HttpURLConnection or OkHttp for SOCKS5.
All products share one account, one prepaid balance and the same gateway, so you can switch type by changing the product, not the code.
From $0.23 per GB
Rotating and sticky home IPs for Java crawlers and testing frameworks.
View residential proxiesFrom $0.33 per IP per month
Fixed provider-registered IPs for services that allowlist a single address.
View static isp proxiesFrom $0.13 per GB
Sessions of up to 12 hours on one ISP IP with unlimited concurrent connections.
View long-acting isp proxiesBuilding a scraper? The web scraping use case covers request rates, retries and product choice in more depth.
Short answers to what people ask most about using a proxy with Java.
The JDK disables Basic authentication for HTTPS tunnels by default. Start the JVM with -Djdk.http.auth.tunneling.disabledSchemes= (an empty value) so the Authenticator can send the username and token.
Use HttpClient.newBuilder().proxy(ProxySelector.of(new InetSocketAddress("gate.firstproxy.shop", 8000))) and add an Authenticator that returns your username and token for proxy requests.
No. OkHttp sends its own CONNECT requests and uses its proxyAuthenticator to add the Proxy-Authorization header, so the JDK property does not affect it.
No, it supports HTTP proxies only. Use HttpURLConnection with Proxy.Type.SOCKS and a default Authenticator, or OkHttp with a SOCKS proxy.
http.Transport with http.ProxyURL, url.UserPassword, socks5 URLs and keep-alive notes.
Programming languagesNode.js proxy setupfetch with undici ProxyAgent, axios with https-proxy-agent, and SOCKS5 agents.
Programming languagesPHP proxy setupcURL extension with CURLOPT_PROXY and CURLOPT_PROXYUSERPWD, plus Guzzle and Laravel.
Phones and tabletsAndroid proxy setupWi-Fi proxy fields, the Chrome sign-in, adb and emulator flags, and app-wide options.
Get your token and run the first Java request
Create an account, top up from $40 with crypto, and copy the gateway, username and token from the dashboard.