Forum Stats

  • 3,853,894 Users
  • 2,264,290 Discussions
  • 7,905,498 Comments

Discussions

Java 12 & 13 ProxySelector

The following code returns the expected results (a list of proxies with the proxy type being Type.HTTP or Type.SOCKS accordingly for each) when run while a proxy is in use and using Java SE 8 update 201.  However, if we use Java SE 12 or 13 this code ALWAYS returns only one proxy record, and that proxy is reported as Type.DIRECT (even though we are running in an environment behind a proxy).

Is there something that has changed and is required in order for the ProxySelector.getDefault().select() method to work with Java SE 12 & 13 and return the same information as when called when using Java SE 8?

            try {                                System.setProperty("java.net.useSystemProxies","true");                URL configURL = "https://www.google.com";                URI configURI = null;                try {                    configURI = configURL.toURI();                }                catch (Exception ex2) {                    System.out.println("Unable to convert configURL to URI.  Error: " + ex2.toString());                }                boolean bFoundProxyInfo = false;                List proxyList = ProxySelector.getDefault().select(configURI);                for (Iterator iter = proxyList.iterator(); iter.hasNext();) {                    Proxy proxy = (Proxy)iter.next();                    System.out.println("Proxy: " + proxy);                    System.out.println("Proxy type: " + proxy.type());                    InetSocketAddress addr = (InetSocketAddress)proxy.address();                    if (addr != null) {                        bFoundProxyInfo = true;                        host = addr.getHostName();                        port = String.valueOf(addr.getPort());                        System.out.println("Proxy hostname: " + host);                        System.out.println("Proxy port: " + port);                    }                    if (bFoundProxyInfo) break;                }                        }            catch (Exception ex) {                 System.out.println("Unable to find proxy information using ProxySelector.  Error: " + ex.toString());            }