Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Java 12 & 13 ProxySelector

Rob_MiltonMar 6 2020

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());

            }

Comments
Post Details
Added on Mar 6 2020
0 comments
210 views