Mobile Proxy for Java
Java 11+ ships java.net.http.HttpClient, which takes a ProxySelector plus an Authenticator for proxy credentials. OkHttp wires the same with a Proxy and a proxyAuthenticator. This guide configures both for a mobile proxy and rotates the IP.
Prerequisites
- →Java 11+ (
java --version). Thejava.net.httpclient is built in. - →A mobile proxy slot, port, username, password and API key from mobileproxies.org.
- →For OkHttp: add
com.squareup.okhttp3:okhttpto your build.
Step-by-Step Configuration
Allow Basic proxy auth on the JDK client
Java disables Basic auth over tunneled HTTPS by default. Set this system property once at startup (or pass -D on the command line):
// Run before building the HttpClient
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
// or: java -Djdk.http.auth.tunneling.disabledSchemes="" -jar app.jarHttpClient with ProxySelector + Authenticator
import java.net.*;
import java.net.http.*;
String host = System.getenv("MP_HOST"); // proxy.mobileproxies.org
int port = Integer.parseInt(System.getenv("MP_HTTP_PORT")); // 8000
String user = System.getenv("MP_USER");
String pass = System.getenv("MP_PASS");
Authenticator proxyAuth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// Only answer challenges from the proxy, not the target site
if (getRequestorType() == RequestorType.PROXY) {
return new PasswordAuthentication(user, pass.toCharArray());
}
return null;
}
};
HttpClient client = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress(host, port)))
.authenticator(proxyAuth)
.connectTimeout(java.time.Duration.ofSeconds(20))
.build();Send a request
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://example.com"))
.GET()
.build();
HttpResponse<String> response =
client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("status: " + response.statusCode());OkHttp alternative
import okhttp3.*;
import java.net.*;
Proxy proxy = new Proxy(
Proxy.Type.HTTP,
new InetSocketAddress(host, port));
OkHttpClient client = new OkHttpClient.Builder()
.proxy(proxy)
.proxyAuthenticator((route, response) -> {
String credential = Credentials.basic(user, pass);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
})
.build();
Request request = new Request.Builder()
.url("https://example.com")
.build();
try (Response response = client.newCall(request).execute()) {
System.out.println("status: " + response.code());
}Verify It Works
Fetch api.ipify.org through the client and print the egress IP. It should be a carrier-owned mobile IP, never your machine's real address.
HttpRequest ipReq = HttpRequest.newBuilder()
.uri(URI.create("https://api.ipify.org"))
.build();
String ip = client.send(ipReq, HttpResponse.BodyHandlers.ofString()).body();
System.out.println("Egress IP: " + ip); // e.g. 100.42.x.x on a mobile ASNRotate the IP
Request a fresh carrier IP with a POST to the switch endpoint. This call goes direct (a default client, not the proxied one) and authenticates with your API key:
String slot = System.getenv("MP_SLOT");
String apiKey = System.getenv("MP_API_KEY");
HttpClient direct = HttpClient.newHttpClient();
HttpRequest rotate = HttpRequest.newBuilder()
.uri(URI.create(
"https://buy.mobileproxies.org/api/v1/proxies/" + slot + "/switch"))
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> r =
direct.send(rotate, HttpResponse.BodyHandlers.ofString());
System.out.println("rotate -> " + r.statusCode());
Thread.sleep(4000); // let the new IP bindTroubleshooting
Proxy auth ignored on HTTPS (407 loop)
The JDK blocks Basic auth on HTTPS tunnels by default. Clear jdk.http.auth.tunneling.disabledSchemes as shown in Step 01, before the HttpClient is built.
Authenticator answers the target site too
Guard with getRequestorType() == RequestorType.PROXY so your proxy credentials are never sent to the destination server as a 401 response.
IP doesn't change after a rotate
The JDK pools connections. Build a fresh HttpClient after switching, or wait the few seconds for the old keep-alive sockets to expire before the next request.
Related Guides
Run Java Through Mobile IPs
$5 trial. Wire up the ProxySelector, point it at a carrier IP, and rotate on the API.