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. Use okhttp3.OkHttpClient.Builder().proxy(Proxy(Proxy.Type.HTTP, InetSocketAddress(host, port))).proxyAuthenticator(...).
Java supports SOCKS via system properties: -DsocksProxyHost=gw.knoxproxy.com -DsocksProxyPort=7001.
Configure RestTemplate or WebClient with a custom HttpClient that includes KnoxProxy proxy settings.
Apache HttpClient 5.x supports proxy via HttpClientBuilder.create().setProxy(new HttpHost("gw.knoxproxy.com", 7000)).
Free trial on rotating residential -- 5 minutes setup, no credit card.