如何配置 Java 的代理设置?

发布于 2024-12-05 04:20:24 字数 990 浏览 1 评论 0原文

我正在尝试打开一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪少女 2024-12-12 04:20:24

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

尘曦 2024-12-12 04:20:24

Sun(呃,Oracle)Java SE 6 Java 网络和代理页面涵盖了这些属性。

The Sun (er, Oracle) Java SE 6 Java Networking and Proxies page covers these properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文