27

I need to automatically detect if a user requires a proxy to access the internet. Is there a way for a Java application to read the systems proxy setting?

Thanks, Jimmy

2 Answers 2

36

Java SE 1.5 provides ProxySelector class to detect the proxy settings. If there is a Direct connection to Internet the Proxy type will be DIRECT else it will return the host and port.

Example below illustrates this functionality:

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.util.Iterator;
import java.util.List;

public class TestProxy {

    public static void main(String[] args) {
        try {
            System.setProperty("java.net.useSystemProxies","true");
            List<Proxy> l = ProxySelector.getDefault().select(
                        new URI("http://www.yahoo.com/"));

            for (Iterator<Proxy> iter = l.iterator(); iter.hasNext(); ) {

                Proxy proxy = iter.next();

                System.out.println("proxy hostname : " + proxy.type());

                InetSocketAddress addr = (InetSocketAddress)proxy.address();

                if(addr == null) {

                    System.out.println("No Proxy");

                } else {
                    System.out.println("proxy hostname : " + addr.getHostName());
                    System.out.println("proxy port : " + addr.getPort());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
3
  • 4
    I am getting: proxy hostname : DIRECT No Proxy Does this mean I am not behind a Proxy Server? Feb 22, 2014 at 10:53
  • Don't forget to import java.util.* and java.net.* for this code. Apr 15, 2014 at 22:24
  • A heads-up - you'll need to import these import java.net.InetSocketAddress; import java.net.Proxy; import java.net.ProxySelector; import java.net.URI; import java.util.Iterator; import java.util.List;
    – mycowan
    Nov 16, 2015 at 0:50
29

The other, accepted, answer is undoubtedly excellent and correct but I thought I would add something here...

If you are on a machine that is configured with "auto detect proxy settings", which I believe is called PAC, the code to detect the proxy in the answer using the Java gubbins will not work (it will think it is a "direct" connection).

There is a library called proxy vole (new BSD license I think), however, that you can use instead so here's the other answer's code slightly modified to use that:

public class testProxy {    
    public static void main(String[] args) {
        try {    
            System.setProperty("java.net.useSystemProxies","true");

            // Use proxy vole to find the default proxy
            ProxySearch ps = ProxySearch.getDefaultProxySearch();
            ps.setPacCacheSettings(32, 1000*60*5);                             
            List l = ps.getProxySelector().select(
                    new URI("http://www.yahoo.com/"));

            //... Now just do what the original did ...
            for (Iterator iter = l.iterator(); iter.hasNext(); ) {
                Proxy proxy = (Proxy) iter.next();

                System.out.println("proxy hostname : " + proxy.type());
                InetSocketAddress addr = (InetSocketAddress)
                    proxy.address();

                if(addr == null) {    
                    System.out.println("No Proxy");    
                } else {
                    System.out.println("proxy hostname : " + 
                            addr.getHostName());

                    System.out.println("proxy port : " + 
                            addr.getPort());    
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }        
    }
}

It needs these imports:

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.util.Iterator;
import java.util.List;

import com.btr.proxy.search.ProxySearch;

Oh, and there're usage examples for proxy vole here.

1
  • Perfect, this is what I was looking for since we are using the PAC script. Some reason I had to set the proxy host/port programmatically after using your code than passing in as VM arguments in eclipse.
    – yonikawa
    Nov 16, 2015 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.