集成

将Shifter与以下工具配合使用 Java

在几分钟内将 Shifter 的住宅和 ISP 代理接入 Java 应用程序。兼容内置 HttpClient(Java 11+)、OkHttp、Apache HttpClient、Selenium 和 Jsoup——无需 SDK。

快速入门

安装

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

基本用法

import java.net.*;
import java.net.http.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Quickstart {
  public static void main(String[] args) throws Exception {
    Authenticator auth = new Authenticator() {
      @Override
      protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(
          "customer-USERNAME-country-us-sid-123ABC",
          "PASSWORD".toCharArray());
      }
    };

    HttpClient client = HttpClient.newBuilder()
      .proxy(ProxySelector.of(new InetSocketAddress("p.shifter.io", 443)))
      .authenticator(auth)
      .build();

    HttpRequest request = HttpRequest.newBuilder()
      .uri(URI.create("https://ipinfo.io/json"))
      .GET()
      .build();

    HttpResponse<String> response =
      client.send(request, HttpResponse.BodyHandlers.ofString());

    System.out.println(response.body());
  }
}

功能特性

开箱即用,支持 HttpClient(Java 11+)、OkHttp、Apache HttpClient、RestTemplate 和 WebClient
默认按请求轮换,使用`sid`实现粘性会话,使用`ttl-N`实现N秒定时固定
通过 Selenium WebDriver 和 Playwright for Java 支持无头浏览器
同一网关端点支持 HTTP、HTTPS 和 SOCKS5 协议
通过用户名参数在 195+ 个国家/地区进行地理定位 — 无需额外依赖
兼容 Java 8、11、17 和 21——支持 Maven、Gradle 以及任何 JVM 生态系统(Kotlin、Scala、Groovy)

示例

内置 HttpClient(Java 11+)

自 Java 11 起的标准库——无需第三方依赖。通过 ProxySelector 配置代理,通过 Authenticator 配置凭据。同一客户端可安全地在多线程间共享。

import java.net.*;
import java.net.http.*;
import java.time.Duration;

public class HttpClientExample {
  static final String PROXY_HOST = "p.shifter.io";
  static final int    PROXY_PORT = 443;

  static HttpClient client(String country, String sid) {
    Authenticator auth = new Authenticator() {
      @Override
      protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(
          String.format("customer-USERNAME-country-%s-sid-%s-ttl-300", country, sid),
          "PASSWORD".toCharArray());
      }
    };

    return HttpClient.newBuilder()
      .proxy(ProxySelector.of(new InetSocketAddress(PROXY_HOST, PROXY_PORT)))
      .authenticator(auth)
      .connectTimeout(Duration.ofSeconds(10))
      .build();
  }

  public static void main(String[] args) throws Exception {
    HttpClient c = client("uk", "456DEF");

    for (String path : new String[]{"/login", "/dashboard", "/orders"}) {
      HttpRequest req = HttpRequest.newBuilder()
        .uri(URI.create("https://example.co.uk" + path))
        .header("User-Agent", "Mozilla/5.0 (Windows) AppleWebKit/537.36")
        .timeout(Duration.ofSeconds(30))
        .GET()
        .build();

      HttpResponse<String> r = c.send(req, HttpResponse.BodyHandlers.ofString());
      System.out.println(path + " " + r.statusCode() + " " + r.body().length());
    }
  }
}

OkHttp

OkHttp 是 Java 中最流行的 HTTP 客户端。在客户端构建器上配置代理和凭据——同一客户端可在多线程间安全复用并多路复用连接。

import okhttp3.*;
import java.net.InetSocketAddress;
import java.net.Proxy;

public class OkHttpExample {
  public static void main(String[] args) throws Exception {
    Proxy proxy = new Proxy(Proxy.Type.HTTP,
      new InetSocketAddress("p.shifter.io", 443));

    Authenticator proxyAuth = (route, response) -> {
      String credential = Credentials.basic(
        "customer-USERNAME-country-de-city-berlin-sid-789GHI", "PASSWORD");
      return response.request().newBuilder()
        .header("Proxy-Authorization", credential)
        .build();
    };

    OkHttpClient client = new OkHttpClient.Builder()
      .proxy(proxy)
      .proxyAuthenticator(proxyAuth)
      .build();

    Request request = new Request.Builder()
      .url("https://api.example.de/products")
      .header("User-Agent", "Mozilla/5.0 (Linux) AppleWebKit/537.36")
      .build();

    try (Response response = client.newCall(request).execute()) {
      System.out.println(response.code() + " " + response.body().string().length());
    }
  }
}

Selenium WebDriver

通过 Shifter 驱动真实的 Chrome/Firefox 实例,用于处理 JavaScript 渲染的目标页面。使用 selenium-wire(或 Chrome 扩展)处理代理认证。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.Proxy;

public class SeleniumExample {
  public static void main(String[] args) {
    Proxy proxy = new Proxy();
    proxy.setHttpProxy("p.shifter.io:443");
    proxy.setSslProxy("p.shifter.io:443");

    ChromeOptions options = new ChromeOptions();
    options.setProxy(proxy);
    options.addArguments("--headless=new");
    options.addArguments(
      "--user-agent=Mozilla/5.0 (Macintosh) AppleWebKit/537.36"
    );

    // For username-password proxy auth, pair this with Shifter's IP
    // whitelist or use selenium-wire — Chrome ignores credentials in
    // headless mode without a sidecar extension.

    WebDriver driver = new ChromeDriver(options);
    try {
      driver.get("https://example.com");
      System.out.println(driver.getTitle());
      System.out.println(driver.getPageSource().length() + " bytes");
    } finally {
      driver.quit();
    }
  }
}

Spring Boot RestTemplate / WebClient

将 Shifter 封装在 Spring @Configuration 中,使应用注入的每个 RestTemplate 或 WebClient 都通过代理路由。兼容 Spring 5+ 和 Spring Boot 3。

import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.client5.http.auth.*;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ShifterRestConfig {
  @Bean
  public RestTemplate shifterRestTemplate() {
    HttpHost proxy = new HttpHost("http", "p.shifter.io", 443);

    BasicCredentialsProvider creds = new BasicCredentialsProvider();
    creds.setCredentials(
      new AuthScope(proxy),
      new UsernamePasswordCredentials(
        "customer-USERNAME-country-fr-sid-ABC123",
        "PASSWORD".toCharArray()));

    CloseableHttpClient httpClient = HttpClients.custom()
      .setProxy(proxy)
      .setDefaultCredentialsProvider(creds)
      .setDefaultRequestConfig(RequestConfig.custom().build())
      .build();

    HttpComponentsClientHttpRequestFactory factory =
      new HttpComponentsClientHttpRequestFactory(httpClient);

    return new RestTemplate(factory);
  }
}
常见问题

常见问题

关于将 Shifter 与 Java 搭配使用的常见问题。

将 ProxySelector.of(new InetSocketAddress("p.shifter.io", 443)) 传入 HttpClient.newBuilder().proxy(...),并通过 Authenticator 提供凭据,该 Authenticator 在 getPasswordAuthentication 中返回 PasswordAuthentication。仅使用标准库,无需额外依赖。

立即开始

开始将Shifter与以下工具配合使用 Java

在不到 5 分钟内将 Shifter 的 205M+ 住宅及 ISP 代理添加到您的 JVM 技术栈中。支持 HttpClient、OkHttp、Spring 和 Selenium WebDriver。

免费试用 Shifter几分钟内完成设置,随时可取消。