Loading...

Using proxy within Java for HTTP or HTTPS connection

:heavy_exclamation_mark: This post is older than a year. Consider some information might not be accurate anymore. :heavy_exclamation_mark:

This post list all essential property names for using HTTP or HTTPS connections in Java. For a more detailed explanation, visit the Java Networking and Proxies Documentation.

These basic property names are used within Java. For HTTPS just add the S to it.

public static final String HTTP_PROXY_HOST = "http.proxyHost";
public static final String HTTP_PROXY_PORT = "http.proxyPort";
public static final String HTTP_PROXY_USER = "http.proxyUser";
public static final String HTTP_PROXY_PASSWORD = "http.proxyPassword";

Set properties at runtime

System.getProperties().put("http.proxyHost", "ProxyURL");
System.getProperties().put("http.proxyPort", "ProxyPort");
System.getProperties().put("http.proxyUser", "UserName");
System.getProperties().put("http.proxyPassword", "Password");

Pass as program arguments

$> java -Dhttp.proxyHost=proxyhostURL -Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=UserName -Dhttp.proxyPassword=Password ProxyClassHere

Remove or cleanup the proxy setting

System.clearProperty(HTTP_PROXY_HOST);
System.clearProperty(HTTP_PROXY_PORT);
System.clearProperty(HTTP_PROXY_USER);
System.clearProperty(HTTP_PROXY_PASSWORD);

Update: 2019-07-11 - add no proxy example

Exclude a list of hosts that should not use the defined proxy.

-Dhttp.nonProxyHosts="localhost|cinhtau.net|cinhtau.de"
Please remember the terms for blog comments.