如何从Java系统设置中获取代理设置 [英] How to get proxy settings from system settings in Java

查看:127
本文介绍了如何从Java系统设置中获取代理设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何在Windows下使用Java获取系统代理信息的方式,但我发现只有一种方法。但它对我不起作用。

I'm looking form way how to get system proxy information in Java under Windows, but I've found just one way. But it does not work for me.

public static void main(String[] args) throws Throwable {
  System.setProperty("java.net.useSystemProxies", "true");
  System.out.println("detecting proxies");
  List<Proxy> pl = ProxySelector.getDefault().select(new URI("http://ihned.cz/"));
  for (Proxy p : pl)
    System.out.println(p);
  Proxy p = null;
  if (pl.size() > 0) //uses first one
    p = pl.get(0);
  System.out.println(p.address());
  System.out.println("Done");
}

当我运行程序时,我得到:

When I run the program, I get:

detecting proxies
DIRECT
null
Done

Java意味着,我直接坐在互联网上。但这是错的。我落后于代理人。我无法为我的计算机获得解决方案。

Java means, that I'm situated directly on internet. But it's wrong. I'm behind proxy. I'm unable to get the solution for my computer.

推荐答案

正如我们在评论中所讨论的,代理设置仅适用于您使用的一些浏览器。

As we discussed in the comments the proxy settings is just applied for some of browsers you use.

如果您希望Java使用相同的设置,您需要手动将其放入java网络设置中(检查这个网页了解详情。)

If you want Java to use the same settings you need to manually put it into the java network settings (check this web page for details).

这篇关于如何从Java系统设置中获取代理设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆