How to get proxy settings from system settings in

2019-04-06 09:58发布

问题:

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 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.

回答1:

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

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).



回答2:

Thanks to Dacwe. The problem is, that browser does not use any system proxy, but it sets proxy self using a script. Thus there are not any proxies in the system and Java cannot reach them.