統合

Shifterと一緒に使用する Java

ShifterのレジデンシャルおよびISP Proxiesを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`、N秒間のタイムドピンには`ttl-N`を使用
Selenium WebDriverおよびPlaywright for Javaによるヘッドレスブラウザをサポート
同一のゲートウェイエンドポイントでHTTP、HTTPS、SOCKS5プロトコルに対応
ユーザー名パラメータを使用した 195+ か国での Geo ターゲティング -- 追加の依存関係不要
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

JavaScriptがレンダリングされる対象に対し、Shifterを通じて実際のChrome / Firefoxインスタンスを操作できます。Proxy認証の処理には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

Spring @ConfigurationでShifterをラップして、アプリがインジェクトするすべての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);
  }
}
FAQ

よくある質問

Shifter と Java の併用に関するよくある質問。

ProxySelector.of(new InetSocketAddress("p.shifter.io", 443))をHttpClient.newBuilder().proxy(...)に渡し、getPasswordAuthenticationに対してPasswordAuthenticationを返すAuthenticatorを通じて認証情報を提供してください。標準ライブラリのみ使用 -- 追加の依存関係は不要です。

new Proxy(Proxy.Type.HTTP, address)でOkHttpClient.Builder()にプロキシを設定し、Credentials.basic(user, pass)を使ってProxy-Authorizationヘッダーを追加するproxyAuthenticatorを提供してください。同じクライアントはスレッド間で安全に再利用できます。

はい。SeleniumのProxyオブジェクトにHTTPおよびSSLプロキシホストを設定し、ChromeOptionsに適用してChromeDriverを起動します。ヘッドレスモードでのユーザー名とパスワード認証には、ShifterのIPホワイトリストと組み合わせるか、selenium-wireを使用してください。ChromeはネイティブでプロキシのCredentialをプロンプトしません。

プロキシのユーザー名にセッションIDを追加してください。例:`customer-USERNAME-country-us-sid-123ABC`。同じsidで認証するすべてのリクエストは同じレジデンシャルIPを再利用します。`ttl-N`を追加すると、そのIPをN秒まで固定できます。

はい。ShifterをプロキシとしてApache HttpClientまたはReactor NettyクライアントをラップするRestTemplateまたはWebClient @Beanを設定してください。外部APIを呼び出す必要がある場所にそのbeanをインジェクトすれば、Spring Framework 5+およびSpring Boot 3で動作します。

SDKは不要です。ShifterはプレーンなHTTP / SOCKS5に対応しています。既存のクライアントを`p.shifter.io:443`に向けるだけです。これによりpom.xmlやbuild.gradleをスリムに保ち、Java 8からJava 21まで動作します。

始める

でShifterを使い始める Java

Shifterの2億500万件以上のレジデンシャル・ISPプロキシを5分以内にJVMスタックに追加できます。HttpClient・OkHttp・Spring・Selenium WebDriverに対応しています。

Shifterを無料で試す数分でセットアップ完了。いつでもキャンセル可能。