Pass Through a Corporate Proxy in Java
1 min read

Pass Through a Corporate Proxy in Java

Pass Through a Corporate Proxy in Java

TL;DR: There's no easy way to create a proxy :)

I've tried for a while to build a way to request something through a proxy, for testing an emulated android app. The solution came from Octavian who kindly provided me a snippet to run once per app lifetime. It tackles the problem from two fronts:

  1. Sets proxy system properties (http.proxy* and https.proxy*)
  2. Sets the default Authenticator

The code is:

public class ProxyUtils {

    public static void authenticate() {

        // Bypass on a certain host
        try {
            if (!InetAddress.getLocalHost().toString().toLowerCase().contains("local")) {
        } catch (UnknownHostException e) {
            getLogger(ProxyUtils.class.getName()).log(SEVERE, null, e);

        // System properties
        setProperty("http.proxyHost", ""); // your proxy IP
        setProperty("http.proxyPort", "88"); // your proxy port
        setProperty("http.proxyUser", "user");
        setProperty("http.proxyPassword", "password");
        setProperty("https.proxyHost", getProperty("http.proxyHost"));
        setProperty("https.proxyPort", getProperty("http.proxyPort"));
        setProperty("https.proxyUser", getProperty("http.proxyUser"));
        setProperty("https.proxyPassword", getProperty("http.proxyPassword"));

        // Authenticator
        Authenticator.setDefault(new Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("user", "password".toCharArray());

One thing that can be improved would be a configuration-based (e.g. defined environment variable) bypass code, rather than a hostname approach.

and the full code (with imports and such) is in this Gist:

NOTE: All credit goes to Tavi for finding and building this.