如何获取托管网络中代理服务器的名称或 ip?

How to get the name or ip of a proxy server in a managed network?

我需要能够以编程方式访问某些 url。我按如下方式使用 URLConnection

URL url = new URL(http,         
                  myProxy.com, // I need to know this parameter   
                  -1,              
                  http://www.example.com/);    

如何获取托管网络中使用的代理服务器的名称。

当我使用像 chrome 这样的浏览器时,它会将我连接到向互联网发出请求的代理服务器。我如何获得代理服务器的名称?

您可以尝试使用 java ProxySelector class to do it, her is short example of it'usage from java proxy configuration 指南:

private Proxy findProxy(URI uri)
{
   try
   {
      ProxySelector selector = ProxySelector.getDefault();
      List<Proxy> proxyList = selector.select(uri);
      if (proxyList.size() > 1)
           return proxyList.get(0);
   }
   catch (IllegalArgumentException e)
   {
   }
   return Proxy.NO_PROXY;
}

要获取主机名和 IP 地址,您可以使用 InetSocketAddress,您可以从 Proxy 实例中获取它:

InetSocketAddress addr = (InetSocketAddress) proxy.address(); 
if(addr != null) { 
  System.out.println("proxy hostname : " + addr.getHostName()); 
  System.out.println("proxy port : " + addr.getPort()); 
} 

但据我所知,需要设置一个系统属性才能做到:

System.setProperty("java.net.useSystemProxies","true");

另一种解决方案是使用 proxy-vole library to do it. Here is some usage examples