How to configure proxy settings for Java?
I am trying to open a URL and read the website line by line. I can do this fine in Eclipse because I guess Eclipse configures it automatically for you. When I try to run the program from the command line the program hangs and never reads the URL.
After some research the problem has to do with the proxy settings, I figured out. All articles I come across say to change something like this:
System.setProperty("java.net.useSystemProxies","true");
Or to add lines of code like this:
System.setProperty("http.proxyHost", "webcache.mydomain.com");
System.setProperty("http.proxyPort", "80");
But I have no idea what to put for my proxy settings and what any of those System.setProperty
calls do. Does anyone know how to set the proxy settings? I am just trying to run this from my home computer on a localhost and I'm not even behind a proxy or anything.
Here is the code I use that works fine in Eclipse.
URL link = new URL("http://www.yahoo.com");
BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
//Inpu开发者_如何学GotStream in = link.openStream();
String inputLine = "";
int count = 0;
while ((inputLine = in.readLine()) != null) {
site = site + "\n" + inputLine;
}
in.close();
java -Dhttp.proxyHost=proxyhostURL
-Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=someUserName
-Dhttp.proxyPassword=somePassword javaClassToRun
The Sun (er, Oracle) Java SE 6 Java Networking and Proxies page covers these properties.
精彩评论