新品不限流量住宅代理:固定价格,流量不计量。 低至 $33.25/天

Java · 集成指南

如何在 Java 中使用代理

Java 11 及更高版本内置的 HttpClient,只需修改一项 JDK 默认设置,即可支持需要认证的代理。本指南涵盖该项设置、粘性会话、地理定位以及网关错误的重试。

  • 共 5 步
  • 10 分钟完成配置
  • 复制即用的代码

开始之前

  • Java 11 或更高版本(java.net.http.HttpClient)
  • 无需任何外部库
  • Proxonym 住宅代理或移动代理套餐
获取代理凭据

已是客户?您的用户名和密码就在控制台中。

  1. 检查 JDK 版本

    Java 11 及更高版本自带 HttpClient。Java 11 还可以直接运行单个源文件,下面的示例就是这样启动的。

    Shell
    java -version
  2. 允许代理隧道使用 Basic 认证

    自 Java 8u111 起,JDK 默认禁止在 HTTPS 隧道中使用 Basic 认证,因此网关登录会失败并返回 407。请在命令行中清空 jdk.http.auth.tunneling.disabledSchemes 属性,或在代码中于创建第一个 HttpClient 之前将其清空。

    Shell
    java -Djdk.http.auth.tunneling.disabledSchemes="" ProxyCheck.java
  3. 通过网关发送请求

    用 ProxySelector 将客户端指向网关,并用 Authenticator 响应代理登录请求。程序会输出目标网站看到的出口 IP。

    Java
    // ProxyCheck.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 ProxyCheck {
        public static void main(String[] args) throws Exception {
            // Must run before the first HttpClient is built
            System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
    
            HttpClient client = HttpClient.newBuilder()
                    .proxy(ProxySelector.of(new InetSocketAddress("gw.proxonym.com", 8000)))
                    .authenticator(new Authenticator() {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            if (getRequestorType() == RequestorType.PROXY) {
                                return new PasswordAuthentication("USERNAME", "PASSWORD".toCharArray());
                            }
                            return null;
                        }
                    })
                    .connectTimeout(Duration.ofSeconds(10))
                    .build();
    
            HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.ipify.org"))
                    .timeout(Duration.ofSeconds(60))
                    .build();
    
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Exit IP: " + response.body());
        }
    }
  4. 轮换、保持或定位 IP

    每个 HttpClient 都有各自的连接和登录状态,而网关按连接分配 IP。需要新 IP 时就新建客户端;需要最长 120 分钟的粘性 IP 时,每个 -session- ID 对应一个客户端;需要定位时则使用位置参数。

    Java
    // Sessions.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;
    import java.util.UUID;
    
    public class Sessions {
        static HttpClient clientFor(String username) {
            return HttpClient.newBuilder()
                    .proxy(ProxySelector.of(new InetSocketAddress("gw.proxonym.com", 8000)))
                    .authenticator(new Authenticator() {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return getRequestorType() == RequestorType.PROXY
                                    ? new PasswordAuthentication(username, "PASSWORD".toCharArray())
                                    : null;
                        }
                    })
                    .connectTimeout(Duration.ofSeconds(10))
                    .build();
        }
    
        static String exitIp(HttpClient client) throws Exception {
            HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.ipify.org"))
                    .timeout(Duration.ofSeconds(60))
                    .build();
            return client.send(request, HttpResponse.BodyHandlers.ofString()).body();
        }
    
        public static void main(String[] args) throws Exception {
            System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
    
            // Rotating: a new client opens a new connection, so each request gets a new IP
            for (int i = 0; i < 3; i++) {
                System.out.println("rotating " + exitIp(clientFor("USERNAME-country-us")));
            }
    
            // Sticky: one client and one session ID keep the same IP for up to 30 minutes
            String session = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
            HttpClient sticky = clientFor("USERNAME-country-us-session-" + session + "-lifetime-30");
            for (int i = 0; i < 3; i++) {
                System.out.println("sticky " + exitIp(sticky));
            }
    
            // Geo-targeting: London, United Kingdom
            System.out.println("london " + exitIp(clientFor("USERNAME-country-gb-city-london")));
        }
    }
  5. 重试网关错误

    隧道被拒绝时会抛出 IOException,例如 Tunnel failed, got: 502;登录被拒绝则最终会以 too many authentication attempts 告终。遇到 429、502 和 504 时,短暂退避后重试;遇到 402、403 或登录失败时则应停止,因为重试无法解决这些问题。

    Java
    static HttpResponse<String> sendWithRetry(HttpClient client, HttpRequest request, int tries)
            throws IOException, InterruptedException {
        for (int attempt = 1; ; attempt++) {
            try {
                HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
                int status = response.statusCode();
                if ((status != 429 && status < 500) || attempt == tries) {
                    return response;
                }
            } catch (IOException e) {
                String message = String.valueOf(e.getMessage());
                System.err.println("attempt " + attempt + ": " + message);
                // 402 (balance), 403 (blocked target) and a rejected login will not go away on a retry
                boolean permanent = message.matches(".*got: 40[23].*") || message.contains("authentication");
                if (permanent || attempt == tries) {
                    throw e;
                }
            }
            Thread.sleep(1000L * attempt);
        }
    }

常见问题

为什么凭据正确,Java 仍然收到 407?

JDK 默认禁止在 HTTPS 隧道中使用 Basic 认证,因此 CONNECT 请求根本不会用到 Authenticator。请在创建第一个 HttpClient 之前将 jdk.http.auth.tunneling.disabledSchemes 设为空值,并确保在请求方类型为 PROXY 时,您的 Authenticator 会返回凭据。

可以用 OkHttp 代替 HttpClient 吗?

可以。为 OkHttpClient 的 Builder 传入一个类型为 HTTP、指向 gw.proxonym.com:8000 的 java.net.Proxy,并设置一个 proxyAuthenticator,用 Credentials.basic 生成 Proxy-Authorization 请求头。OkHttp 会自行处理 CONNECT 认证,因此 JDK 的隧道属性对它不起作用,用户名参数的用法也完全相同。

如何让每个请求都使用新 IP?

网关会为每个新连接分配 IP,而 HttpClient 会保持连接打开以便复用。如果每次调用都必须从不同的 IP 发出,请为每个请求新建客户端,或为每个请求分配各自的随机会话 ID。如果需要稳定的粘性 IP,则复用同一个客户端。

“Tunnel failed, got: 502”是什么意思?

这是 HttpClient 报告 CONNECT 请求被拒绝的方式,末尾的数字就是网关状态码。502 表示上游节点故障,504 表示目标网站超时,两者通常重试即可成功。402 表示余额已用完或套餐已过期,403 则表示目标网站受可接受使用政策限制而被屏蔽。

准备好接入了吗?立即获取凭据

选择套餐,从控制台复制用户名和密码,然后运行上面的代码。

  • 按量付费
  • 即时开通
  • 支持加密货币
  • 不自动续费