Sous proxy / Integrations / Java HttpClient

Password proxy in Java HttpClient

In Java a password proxy breaks on HTTPS sites because of one system property. Here is how to set up HttpClient properly.

HTTPHTTPSLogin and passwordRotation and sticky
Java HttpClient · proxy
new InetSocketAddress("HOST", PORT)
LOGIN / ••••••
(empty)
✓ IP: Germany, residentialRun

Why use a proxy in Java HttpClient

java.net.http.HttpClient is the built-in HTTP client of Java 11 and newer. The proxy is set via ProxySelector; login and password come from an Authenticator. By default the JDK forbids Basic auth for HTTPS tunnels; the jdk.http.auth.tunneling.disabledSchemes property turns that off.

What to pick

Take an HTTP proxy: HttpClient works only with HTTP-type proxies. For server tasks with a stream of requests rotation fits; for a session with one site, sticky.

Residential

from $0.6 / GB

Home ISP addresses. The main choice for accounts and protected sites.

VIP

$2 / GB

Hand-picked clean IPs, faster and more stable than regular residential.

Mobile

$4 / GB

Carrier IPs, for platforms that check addresses strictly.

Step-by-step setup

Button names are given exactly as they appear in Java HttpClient.

  1. Check the Java version

    Run java -version; you need Java 11 or newer.

  2. Export the proxy

    Take an HTTP proxy in @SousProxyBot or the dashboard: host, port, login and password.

  3. Allow Basic for HTTPS

    Run with the flag -Djdk.http.auth.tunneling.disabledSchemes= (empty value). Without it the login won't reach the proxy when opening HTTPS sites.

  4. Configure the client

    Give HttpClient.newBuilder() the proxy via ProxySelector.of and an Authenticator that answers only PROXY-type requests.

  5. Run and check the IP

    Run java ProxyIp.java (Java 11+ runs a single file without compiling). The console shows JSON with the proxy IP.

Code example

Put in your own HOST, PORT, LOGIN and PASSWORD from the bot or the dashboard.

java
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 ProxyIp {
    public static void main(String[] args) throws Exception {
        // Without this the JDK will not send a Basic login over HTTPS (CONNECT).
        // More reliable as a flag: java -Djdk.http.auth.tunneling.disabledSchemes= ProxyIp
        System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

        HttpClient client = HttpClient.newBuilder()
                .proxy(ProxySelector.of(new InetSocketAddress("HOST", PORT)))
                .authenticator(new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        if (getRequestorType() == RequestorType.PROXY) {
                            return new PasswordAuthentication("LOGIN", "PASSWORD".toCharArray());
                        }
                        return null;
                    }
                })
                .connectTimeout(Duration.ofSeconds(15))
                .build();

        HttpRequest request = HttpRequest.newBuilder(
                URI.create("https://api.ipify.org?format=json")).build();

        HttpResponse<String> response =
                client.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

Tested by running it

We ran this example ourselves: OpenJDK 21.0.13 · java.net.http.HttpClient. Real output below, IP hidden.

Terminal · ProxyIp.java
$ java ProxyIp.java
{"ip":"proxy IP"}

And this is the response when the password is wrong:

$ java ProxyIp.java
…
	at ProxyIp.main(ProxyIp.java:35)
Caused by: java.io.IOException: too many authentication attempts. Limit: 3
	at java.net.http/jdk.internal.net.http.AuthenticationFilter.response(AuthenticationFilter.java:363)
	at java.net.http/jdk.internal.net.http.MultiExchange.responseFilters(MultiExchange.java:254)

Common errors

The issues people most often bring to support.

!

HTTPS sites return 407 while HTTP worksThe JDK forbids Basic for tunnels by default. Run with -Djdk.http.auth.tunneling.disabledSchemes= or set the property in code before creating the client.

!

Empty response or null instead of JSONThe proxy returned 407 with an empty body. Check response.statusCode() and the login and password.

!

A SOCKS proxy is ignoredHttpClient works only with HTTP proxies. Export the proxy with the HTTP protocol.

!

Authenticator hands the login to another siteCheck getRequestorType() == RequestorType.PROXY as in the example, otherwise the password also goes to any server that asks for auth.

Need a proxy for Java HttpClient?

Residential, VIP and mobile IPs. HTTP and SOCKS5, pay per GB, 1 GB free to try.

FAQ

Can I set the proxy globally for the JVM?

Yes, with the https.proxyHost and https.proxyPort properties, but the login still needs an Authenticator. For HttpClient a ProxySelector in the builder is clearer.

Does this work in the old HttpURLConnection?

Same logic: Authenticator.setDefault and the same disabledSchemes property for HTTPS. We recommend the new client though.

How do I get a new IP per request?

Export the proxy in rotation mode; no code changes needed.