Skip to content

Java integration ​

Send requests through an assigned static endpoint. The example reads credentials from the process environment and uses an HTTP proxy for an HTTPS target by default. It does not fetch or rotate IPs.

Before you start ​

Set PROXY_HOST, PROXY_PORT, PROXY_USERNAME, and PROXY_PASSWORD in the terminal, container, or deployment platform running the program. Use the current dashboard details and the port for the selected protocol. Keep actual passwords out of source control, logs, and frontend code.

Runtime and dependencies ​

Use JDK 11+ and add the dependency below to a Maven project. Save the example as src/main/java/ProxyExample.java and run main in your IDE or with the Maven Exec plugin. The example uses the HTTP port with proxy authentication; HTTPS requests use a CONNECT tunnel.

xml
<dependency>
  <groupId>com.squareup.okhttp3</groupId>
  <artifactId>okhttp</artifactId>
  <version>4.12.0</version>
</dependency>

Set up the connection ​

java
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 final class ProxyExample {
    private static String required(String name) {
        String value = System.getenv(name);
        if (value == null || value.isBlank()) {
            throw new IllegalArgumentException("Missing environment variable: " + name);
        }
        return value;
    }

    public static void main(String[] args) {
        String host = required("PROXY_HOST").replace("[", "").replace("]", "");
        int port = Integer.parseInt(required("PROXY_PORT"));
        String credential = Credentials.basic(required("PROXY_USERNAME"), required("PROXY_PASSWORD"));
        OkHttpClient client = new OkHttpClient.Builder()
            .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port)))
            .proxyAuthenticator((route, response) -> {
                if (response.request().header("Proxy-Authorization") != null) return null;
                return response.request().newBuilder()
                    .header("Proxy-Authorization", credential).build();
            })
            .connectTimeout(Duration.ofSeconds(10))
            .readTimeout(Duration.ofSeconds(30))
            .callTimeout(Duration.ofSeconds(40))
            .build();
        Request request = new Request.Builder().url("https://ip123.in/ip.json").build();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                System.err.println("HTTP status: " + response.code());
                System.exit(1);
            }
            if (response.body() != null) System.out.println(response.body().string());
        } catch (java.io.IOException error) {
            System.err.println("Proxy request failed; check network and credentials");
            System.exit(1);
        }
    }
}

This sample is for HTTP proxy access. For SOCKS5 username/password authentication, use an implementation that explicitly supports it. Changing Proxy.Type.HTTP to SOCKS does not reuse the HTTP proxy authenticator.

If something goes wrong ​

If this example succeeds but your workload fails, check the actual request process, SDK proxy configuration, timeouts, and retry policy. Keep TLS certificate verification enabled. The sample prints lookup data or errors without credentials; redact diagnostic logs too. See the FAQ.