Explain Codes LogoExplain Codes Logo

How do I make HttpURLConnection use a proxy?

java
proxy-engineering
best-practices
tools
Alex KataevbyAlex KataevΒ·Feb 12, 2025
⚑TLDR

Kick-off by configuring HttpURLConnection to utilize a proxy with these few lines:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyHost", proxyPort)); // Why did the programmer go broke? Because he used up all his cache. 😁 HttpURLConnection conn = (HttpURLConnection) new URL("http://example.com").openConnection(proxy);

Note: Replace "proxyHost" and proxyPort with your appropriate proxy details.

Proxy settings with JVM properties

Java’s built-in system properties, such as http.proxyHost and http.proxyPort, can be utilized to set proxy settings. HTTPS follows the same format.

System.setProperty("http.proxyHost", "proxyHost"); System.setProperty("http.proxyPort", "proxyPort"); // Behind every good program is a frustrated programmer. True story. 🀣

For HTTPS requests, use https.proxyHost and https.proxyPort.

System.setProperty("https.proxyHost", "proxyHost"); System.setProperty("https.proxyPort", "proxyPort");

Auto proxy detection with JVM

To let your JVM handle proxy detection, simply set java.net.useSystemProxies system property to "true".

// Who says programmers don't have a life? They do, with little debugging! πŸ’»πŸ˜‰ System.setProperty("java.net.useSystemProxies", "true");

Proxy authentication with authenticator

Use Authenticator.setDefault() to handle proxy authentication by supplying your username and password.

Authenticator.setDefault(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username", "password".toCharArray()); } // Why don't programmers like nature? It has too many bugs. πŸ˜„ });

Proxy error handling

Always check the HTTP response code, specifically 407. This code signals proxy authentication errors.

If the response code is HttpURLConnection.HTTP_PROXY_AUTH, this indicates a proxy authentication error.

if (conn.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH) { // Handle authentication failure // Remember to not 'Java' hard time! πŸ˜‚ }

Proxy instance for HttpURLConnection

Creating a Proxy instance with an explicit IP address and port offers more control over which proxies to use.

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.1.100", 8080)); // Connecting you to the world, one proxy at a time! 🌐😎

Check and validate proxy integration

Check your process and system-wide proxy settings. Validate your proxy setup by running a test connection. It’s a good practice to ensure that your setup works as intended.

Maintenance and performance checks

Maintaining good habits in proxy implementation:

  • Avoid hardcoding.
  • Confirm the use of proxy.
  • Review the compatibility of your settings.
  • Keep a check on your security measures.
  • Optimise your network.

Connection using a secure proxy

Ensure data safety with a HttpsURLConnection instance. Interact with a secure proxy and consider the SSL context.

SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); // Securely connecting, because we care! πŸ”πŸ’Ό

Advanced proxy configurations

Consider the following for more complex scenarios:

  • Multiple Authenticator configs.
  • Use of proxy selectors.
  • Use of SOCKS proxies.
  • Use of caching mechanisms.
  • Connection pooling.