Thursday, July 22, 2010

Setting http Proxy for Java Application

There are 2 main ways to set/configure http proxy in your java application.

* As a command line option when invoking the VM
* Using the System.setProperty(String, String) in your code

There are 3 properties you can set to specify the proxy that will be used by the http protocol handler:

* http.proxyHost: the host name of the proxy server
* http.proxyPort: the port number, the default value being 80
* http.nonProxyHosts: a list of hosts that should be reached directly, bypassing the proxy

Example of using command line option to set proxy configuration on java
java -Dhttp.proxyHost=webcache.mydomain.com -Dhttp.proxyPort=8080
-Dhttp.noProxyHosts=”localhost|host.mydomain.com” YourAppMainClass

Example of using code to configure proxy setting on java
//Set the http proxy to webcache.mydomain.com:8080
System.setProperty("http.proxyHost", "webcache.mydomain.com");
System.setPropery("http.proxyPort", "8080");
// Next connection will be through proxy.

There are other proxy settings that you can set as well such as https, ftp, socks.
More resources:
http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html

No comments:

Post a Comment