Java Examples
Java Examples
memu home questions

How to use an HTTP proxy in java?

Set in the command line:

   # java -Dhttp.proxyHost=myproxy -Dhttp.proxyPort=80 JavaClass

Specify a proxy in your source code:

System.setProperty("http.proxyHost", "");
System.setProperty("http.proxyPort", "80");

Specify use of system proxy in source code:

System.setProperty("", "true");

Create a instance of proxy and pass it to the openConnection() method:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("", 8080));
URL url = new URL("");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);

Questions answered by this page:

How do I configure proxy settings for Java? Java HTTP Proxy Settings HTTP Proxy setting in Java How to Set JVM Proxy Setting Without Touching the Code