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

Jimmy
  • 1,433
  • 2
  • 14
  • 19

2 Answers2

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();
        }
    }
}
David Passmore
  • 6,089
  • 4
  • 46
  • 70
Luciano Fiandesio
  • 10,037
  • 10
  • 48
  • 56
  • 4
    I am getting: proxy hostname : DIRECT No Proxy Does this mean I am not behind a Proxy Server? – Koray Tugay Feb 22 '14 at 10:53
  • Don't forget to import java.util.* and java.net.* for this code. – Graham Mitchell Apr 15 '14 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 '15 at 00: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.

Community
  • 1
  • 1
kmp
  • 10,535
  • 11
  • 75
  • 125
  • 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 '15 at 21:44