如何配置 Java 的代理设置?
我正在尝试打开一个 URL 并逐行阅读该网站。我可以在 Eclipse 中很好地完成此操作,因为我猜 Eclipse 会自动为您配置它。当我尝试从命令行运行该程序时,该程序挂起并且从不读取 URL。
经过一番研究,我发现问题与代理设置有关。我遇到的所有文章都说要更改这样的内容:
System.setProperty("java.net.useSystemProxies","true");
或者添加这样的代码行:
System.setProperty("http.proxyHost", "webcache.mydomain.com");
System.setProperty("http.proxyPort", "80");
但我不知道要为我的代理设置添加什么以及这些 System.setProperty
调用的作用。有谁知道如何设置代理设置?我只是想在本地主机上从我的家庭计算机上运行它,而且我什至没有代理或任何东西。
这是我使用的代码,在 Eclipse 中运行良好。
URL link = new URL("http://www.yahoo.com");
BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
//InputStream in = link.openStream();
String inputLine = "";
int count = 0;
while ((inputLine = in.readLine()) != null) {
site = site + "\n" + inputLine;
}
in.close();
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()));
//InputStream in = link.openStream();
String inputLine = "";
int count = 0;
while ((inputLine = in.readLine()) != null) {
site = site + "\n" + inputLine;
}
in.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
java -Dhttp.proxyHost=proxyhostURL
-Dhttp.proxyPort=代理端口号
-Dhttp.proxyUser=someUserName
-Dhttp.proxyPassword=somePassword javaClassToRun
java -Dhttp.proxyHost=proxyhostURL
-Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=someUserName
-Dhttp.proxyPassword=somePassword javaClassToRun
Sun(呃,Oracle)Java SE 6 Java 网络和代理页面涵盖了这些属性。
The Sun (er, Oracle) Java SE 6 Java Networking and Proxies page covers these properties.