Shifter verwenden mit Java
Binden Sie Shifters Residential- und ISP-Proxys in wenigen Minuten in Java-Anwendungen ein. Funktioniert mit dem integrierten HttpClient (Java 11+), OkHttp, Apache HttpClient, Selenium und Jsoup – kein SDK erforderlich.
Schnellstart
Installieren
// Maven
// <dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.12.0</version></dependency> Grundlegende Nutzung
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());
}
} Funktionen
Beispiele
Integrierter HttpClient (Java 11+)
Die Standardbibliothek seit Java 11 – keine Drittanbieter-Abhängigkeiten. Konfigurieren Sie den Proxy über ProxySelector und die Anmeldedaten über Authenticator. Derselbe Client kann sicher über Threads hinweg geteilt werden.
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 ist der beliebteste HTTP-Client in Java. Konfigurieren Sie Proxy und Anmeldedaten am Client-Builder – derselbe Client kann sicher über Threads hinweg wiederverwendet werden und multiplext Verbindungen.
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
Steuern Sie eine echte Chrome/Firefox-Instanz über Shifter für JavaScript-gerenderte Ziele. Verwenden Sie selenium-wire (oder eine Chrome-Erweiterung), um die Proxy-Authentifizierung zu verwalten.
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
Binden Sie Shifter in eine Spring @Configuration ein, damit jedes RestTemplate oder WebClient, das Ihre App injiziert, über den Proxy geleitet wird. Funktioniert mit Spring 5+ und 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);
}
} Häufig gefragt FAQ-Fragen
Häufige Fragen zur Verwendung von Shifter mit Java.
Übergeben Sie ProxySelector.of(new InetSocketAddress("p.shifter.io", 443)) an HttpClient.newBuilder().proxy(...) und stellen Sie Anmeldedaten über einen Authenticator bereit, der PasswordAuthentication für getPasswordAuthentication zurückgibt. Nur Standardbibliothek – keine zusätzlichen Abhängigkeiten.
Shifter verwenden mit Java
Fügen Sie Shifters 205M+ Residential- und ISP-Proxys in unter 5 Minuten zu Ihrem JVM-Stack hinzu. Kompatibel mit HttpClient, OkHttp, Spring und Selenium WebDriver.