Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Windows/IE proxy setting using Java

Tags:

java

proxy

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

like image 304
Jimmy Avatar asked Feb 08 '11 13:02

Jimmy


People also ask

How do I automatically detect proxy settings in Internet Explorer?

On the Tools menu, click Internet Options, and then click Connections. Click Settings or LAN Settings. In the Automatic configuration area, check that you've chosen the Use automatic configuration script box, and that it has the correct location to your automatic configuration script or for your automatic proxy URL.


2 Answers

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();         }     } } 
like image 107
Luciano Fiandesio Avatar answered Sep 21 '22 18:09

Luciano Fiandesio


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.

like image 41
kmp Avatar answered Sep 20 '22 18:09

kmp