Last active
July 28, 2026 09:47
-
-
Save quantumproxies/5595b703b56c8366d2a345fd7d603686 to your computer and use it in GitHub Desktop.
Java 11+ HttpClient through an authenticated proxy — fixes the 407 Basic-auth tunneling gotcha — https://quantumproxies.io/residential-proxies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Java 11+ HttpClient through an authenticated residential proxy. | |
| // | |
| // THE GOTCHA: since JDK 8u111, Basic auth on HTTPS tunnels is disabled by | |
| // default. Without the system property below you get "407 Proxy | |
| // Authentication Required" forever, even with a correct Authenticator. | |
| // | |
| // java -Djdk.http.auth.tunneling.disabledSchemes= JavaProxyExample.java | |
| // | |
| // Proxy: QuantumProxies residential — https://quantumproxies.io/residential-proxies | |
| // Credentials: https://app.quantumproxies.io/register | |
| import java.net.*; | |
| import java.net.http.*; | |
| public class JavaProxyExample { | |
| public static void main(String[] args) throws Exception { | |
| String user = System.getenv().getOrDefault("QP_USER", "YOUR_USERNAME") + "-country-us"; | |
| String pass = System.getenv().getOrDefault("QP_PASS", "YOUR_PASSWORD"); | |
| HttpClient client = HttpClient.newBuilder() | |
| .proxy(ProxySelector.of(new InetSocketAddress( | |
| "residentialboson.quantumproxies.io", 9000))) | |
| .authenticator(new Authenticator() { | |
| @Override protected PasswordAuthentication getPasswordAuthentication() { | |
| return new PasswordAuthentication(user, pass.toCharArray()); | |
| } | |
| }) | |
| .build(); | |
| HttpResponse<String> res = client.send( | |
| HttpRequest.newBuilder(URI.create("https://ipinfo.io/json")).build(), | |
| HttpResponse.BodyHandlers.ofString()); | |
| System.out.println(res.body()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment