分类:
2010-03-13 01:09:56
How to connect to internet from Java program through a proxy? The answer is very simple Just add the following arguments to the virtual machine
java -Dhttp.proxyHost=hostname -Dhttp.proxyPort=port -Dhttp.proxyUser=username -Dhttp.proxyPassword=passowrd
or just put set these System properties using the code.
System.getProperties().put("http.proxyHost", "hostname");
System.getProperties().put("http.proxyPort", "port");
System.getProperties().put("http.proxyUser", "username");
System.getProperties().put("http.proxyPassword", "password");
If your proxy doesn’t require an authentication, you just need to set proxyHost and proxyPort properties only.
But the fist method wouldn’t work if we are running our program from within Eclipse as we don’t have command line access. So to set the proxy properties in Eclipse just go to Run menu and click on Run. A dialog box would appear and in the first tab (Main tab) enter your main class and other information and in the Arguments tab under VM arguments just add the following line.
-Dhttp.proxyHost=hostnmae -Dhttp.proxyPort=port