Skip to content
Nefo

Java

How to Use Residential Proxies with Java HttpClient

Set up a ProxySelector and an Authenticator on java.net.http.HttpClient, clear the JDK's HTTPS tunneling restriction, and route Java requests through Nefo residential proxies.

Before you start

Nefo offers residential proxies across 227 countries over HTTP, HTTPS, and SOCKS5 with username/password auth. Generate credentials for free in the dashboard proxy generator; only routed traffic consumes your prepaid bandwidth (from 5 GB for $4.50, non-expiring on flexible plans).

By default the JDK disables Basic proxy authentication over HTTPS tunnels. Clear jdk.http.auth.tunneling.disabledSchemes so credentials are sent to the proxy.

  • JDK 17+ with java.net.http available
  • System property jdk.http.auth.tunneling.disabledSchemes set to empty
  • A Nefo account with prepaid bandwidth
  • A username/password from /dashboard/generator
  • Your target country token

Java HttpClient proxy and Authenticator example

Build a client with proxy and authenticator:

Then send a request to https://api.ipify.org and read the body. Start the JVM with -Djdk.http.auth.tunneling.disabledSchemes="" so proxy Basic auth works over HTTPS. Change the country token to target any of 227 countries.

shell
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.ProxySelector;
import java.net.http.HttpClient;

public class ProxyClient {
    public static void main(String[] args) {
        HttpClient client = HttpClient.newBuilder()
                .proxy(ProxySelector.of(new InetSocketAddress("gw.nefo.net", 9000)))
                .authenticator(new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("USERNAME-country-us", "PASSWORD".toCharArray());
                    }
                })
                .build();
    }
}

Java rotating vs sticky proxy sessions

Rotating sessions rotate the exit IP each request on HTTP port 9000, well suited to distributing many requests across residential exits.

Sticky sessions hold one IP from 1 minute up to 24 hours on HTTP port 10000. Use a token like USERNAME-country-us-session-svc4-lifetime-45 in the Authenticator username to keep one IP for a 45-minute session.

Geo-targeting in the Java proxy username

Targeting is expressed in the username token: country across 227 countries, US state and city, or ASN/ISP within a country. Only the username string changes between regions.

Use the network lawfully, respect the target site's terms of service, and stay within applicable law and reasonable request rates.

Frequently asked questions

The JDK disables Basic authentication on HTTPS proxy tunnels by default, so the proxy replies 407. Launch the JVM with -Djdk.http.auth.tunneling.disabledSchemes="" (an empty value) so the username token and password are sent.

Start with 5 GB. Keep what you don't use.

Residential bandwidth from $0.53/GB that never expires. Card, PayPal, Apple Pay, Google Pay, Alipay and crypto.