How to use proxy with CURL and HTTP requests from Java code

04 Mar 2024

CURL is easy pizy :

# export corresponding env vars:
export http_proxy=http://HOST:PORT
export https_proxy=http://HOST:PORT

# and execute request to host that requires proxy:
curl https://your-host-dot-com-slash-something

# or you can specify proxy directly in curl:
curl --proxy http://proxy:port https://your-host-dot-something

Java is trickier :

# pass the following arguments to your program:
java \
-Dhttp.proxySet=true \
-Dhttp.proxyHost=host.com \
-Dhttp.proxyPort=port-number \
-Dhttps.proxySet=true \
-Dhttps.proxyHost=host.com \
-Dhttps.proxyPort=port-number \
...
your-app.jar

Here is an example of an app for testing:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;

public class TestIP {

    public static void main(String[] args) throws IOException {
        System.out.println("Args = " + Arrays.toString(args));
        String host = args[0];
        System.out.println("URL = " + host);

        URL url = new URL(host);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("GET");
        int status = connection.getResponseCode();

        System.out.println("Response Status = " + status);
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuffer sb = new StringBuffer();

        String line;
        while((line = br.readLine()) != null) {
            sb.append(line);
        }

        br.close();
        connection.disconnect();
        System.out.println("Response = " + sb);
    }
}

And if you are using AppacheHttpClient you need to make it use system properties:

return HttpClientBuilder
  .create()
  .useSystemProperties()
  ...
  .build();