Java HttpClient with proxy support. Configure KnoxProxy using the built-in java.net.http.HttpClient API with ProxySelector.
Use ProxySelector for HttpClient.
var client = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress("gw.knoxproxy.com", 7000)))
.authenticator(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("USER", "PASS".toCharArray());
}
})
.build();Send HTTP request through proxy.
var request = HttpRequest.newBuilder(URI.create("https://httpbin.org/ip")).build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());Modify username.
"USER-country-us"Run and check exit IP.
Catch IOException.
try { ... } catch (IOException e) { e.printStackTrace(); }HttpClient reuses connections automatically.
import java.net.*;
import java.net.http.*;
import java.io.IOException;
public class KnoxProxy {
public static void main(String[] args) throws IOException, InterruptedException {
var client = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress("gw.knoxproxy.com", 7000)))
.authenticator(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("USER", "PASS".toCharArray());
}
})
.build();
var request = HttpRequest.newBuilder()
.uri(URI.create("https://httpbin.org/ip"))
.build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Exit IP: " + response.body());
}
}HttpClient connection pool may reuse connections. Create a new client per rotation or use -session-{id} in username for explicit session control.
| Problem | Fix |
|---|---|
| ConnectException: Connection refused | Verify gw.knoxproxy.com and port 7000. |
| 407 response | Set up Authenticator in HttpClient builder. |
USER-country-de-city-berlin-session-profile07Order matters -- geo flags before the session flag. The session name is free text; use the profile ID so the mapping is self-documenting. Password stays as issued; no flags belong there. HTTP on :7000, SOCKS5 on :7001, same credentials.
Yes. OkHttp accepts a proxy much like java.net.http.HttpClient does: build an okhttp3.OkHttpClient with .proxy(Proxy(Proxy.Type.HTTP, InetSocketAddress("gw.knoxproxy.com", 7000))), then add .proxyAuthenticator(...) to supply your KnoxProxy username and password, following the same Authenticator pattern shown for the standard HttpClient setup above. This works identically in Kotlin and Android projects that already depend on OkHttp.
Yes, indirectly. HttpClient does not take a SOCKS proxy directly through ProxySelector, but the JVM-wide system properties -DsocksProxyHost=gw.knoxproxy.com and -DsocksProxyPort=7001 route all socket-level traffic, including HttpClient requests, through the KnoxProxy SOCKS5 gateway. Set these as JVM startup flags rather than inside application code for them to take effect.
Configure a RestTemplate or WebClient bean with a custom java.net.http.HttpClient, or Apache HttpClient, that has KnoxProxy set through ProxySelector and an Authenticator, the same pattern used in a plain Java app. Register that client as the Spring bean your services inject, so every outgoing HTTP call in the application automatically routes through KnoxProxy.
Apache HttpClient 5.x supports proxies through HttpClientBuilder.create().setProxy(new HttpHost("gw.knoxproxy.com", 7000)), paired with a CredentialsProvider holding your KnoxProxy username and password for the 407 challenge. This is a common alternative to the built-in java.net.http.HttpClient shown above, especially in older codebases already depending on Apache HttpComponents.
Rotating residential proxies -- 5 minutes setup, instant activation, 14-day money-back guarantee.