Java
Use NyxonProxies from Java with HttpClient and OkHttp: proxy authentication for HTTPS sites, the disabledSchemes setting, rotation, sticky sessions and errors.
Java's built-in HttpClient supports authenticated proxies, but one default blocks them for HTTPS sites: Basic authentication on proxy tunnels is turned off. This page shows the setting that turns it back on, and the same setup with OkHttp, which needs no such setting.
HttpClient
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 ProxyDemo {
public static void main(String[] args) throws Exception {
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
HttpClient client = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress("geo.gw.nyxon.sh", 8080)))
.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType() == RequestorType.PROXY) {
return new PasswordAuthentication("USERNAME", "PASSWORD".toCharArray());
}
return null;
}
})
.connectTimeout(Duration.ofSeconds(30))
.build();
HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.ipify.org")).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode() + " " + response.body());
}
}Run it with java ProxyDemo.java. The authenticator answers only proxy challenges, so your credentials are never sent to a website by mistake.
Country and sticky session
Targeting and sessions go in the username returned by the authenticator. For example, to keep one German IP for up to 10 minutes:
String sid = java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 8);
String user = "USERNAME-country-de-session-" + sid + "-time-10";Return user instead of "USERNAME" in getPasswordAuthentication. Use one client per session. See Rotating and sticky sessions.
A new IP on every request
HttpClient keeps connections open and reuses them, so requests to one site through the same client share an IP. For a new IP per request on a rotating username, build a new client for each request. Since Java 21, close it afterwards with try-with-resources.
SOCKS5
HttpClient does not support SOCKS proxies. Given one through a ProxySelector, it connects directly, from your own IP, without an error. Use the HTTP port: it carries HTTPS sites through an encrypted tunnel, so you lose nothing.
OkHttp
OkHttp, common on Android and in Kotlin projects, sends proxy credentials through a proxyAuthenticator and needs no system property:
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.time.Duration;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkProxyDemo {
public static void main(String[] args) throws Exception {
OkHttpClient client = new OkHttpClient.Builder()
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("geo.gw.nyxon.sh", 8080)))
.proxyAuthenticator((route, response) -> {
if (response.request().header("Proxy-Authorization") != null) {
return null; // the credentials were refused, so give up
}
return response.request().newBuilder()
.header("Proxy-Authorization", Credentials.basic("USERNAME", "PASSWORD"))
.build();
})
.callTimeout(Duration.ofSeconds(30))
.build();
Request request = new Request.Builder().url("https://api.ipify.org").build();
try (Response response = client.newCall(request).execute()) {
System.out.println(response.code() + " " + response.body().string());
}
}
}Errors
| Error | Cause |
|---|---|
Status 407 on every HTTPS request | The disabledSchemes property is missing, or the credentials are wrong. |
ConnectException | Wrong host or port, or a firewall. |
OkHttp: Failed to authenticate with proxy | Wrong username or password. |
More in Error codes and Troubleshooting.
Last updated
Still stuck?
People answer the live chat around the clock. Send the proxy line you used (without the password) and the error you see.