Connecting Through Proxy Servers in Core Java

Proxy servers act as intermediaries between client applications and other servers. In an enterprise setting, we often use them to help provide control over the content that users consume, usually across network boundaries.
In this tutorial, we’ll look at how to connect through proxy servers in Java.
First, we’ll explore the older, more global approach that is JVM-wide and configured with system properties. Afterward, we’ll introduce the Proxy class, which gives us more control by allowing configuration on a per-connection basis.

Set via Command Line Arguments

We can define proxies on the command line by passing in the settings as system properties:

java -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=3128 com.baeldung.networking.proxies.CommandLineProxyDemo

When starting a process in this way, we’re able to simply use openConnection() on the URL without any additional work:

URL url = new URL(RESOURCE_URL);
URLConnection con = url.openConnection();

Set Using System.setProperty(String, String)

If we’re unable to set proxy properties on the command line, we can set them with calls to System.setProperty() within our program:

System.setProperty("http.proxyHost", "127.0.0.1");
System.setProperty("http.proxyPort", "3128");

URL url = new URL(RESOURCE_URL);
URLConnection con = url.openConnection();

If we later unset the relevant system properties manually, then the proxy will no longer be used:

System.setProperty("http.proxyHost", null);

Using an HTTP Proxy

To use an HTTP proxy, we first wrap a SocketAddress instance with a Proxy and type of Proxy.Type.HTTP. Next, we simply pass the Proxy instance to URLConnection.openConnection():

URL weburl = new URL(URL_STRING);
Proxy webProxy
  = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3128));
HttpURLConnection webProxyConnection
  = (HttpURLConnection) weburl.openConnection(webProxy);

Simply put, this means that we’ll connect to URL_STRING, but then route that connection through a proxy server hosted at 127.0.0.1:3128.

Using a SOCKS Proxy

Using a SOCKS proxy is similar to the HTTP variant when working with URLConnection. We start by wrapping a SocketAddress instance with a Proxy using a type of Proxy.Type.SOCKS. Afterward, we pass the Proxy instance to URLConnection.openConnection:

Proxy socksProxy
  = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1080));
HttpURLConnection socksConnection
  = (HttpURLConnection) weburl.openConnection(socksProxy);

It’s also possible to use a SOCKS proxy when connecting to a TCP socket. First, we use the Proxy instance to construct a Socket. Afterward, we pass the destination SocketAddress instance to Socket.connect():

Socket proxySocket = new Socket(socksProxy);
InetSocketAddress socketHost
  = new InetSocketAddress(SOCKET_SERVER_HOST, SOCKET_SERVER_PORT);
proxySocket.connect(socketHost);
complete at 0.0006 s; cached: 1; cache enable: truekf93682461da39f65ea392a0296c68ed0940182bb.html