开发者

Detecting Windows/IE proxy setting using Java

开发者 https://www.devze.com 2023-02-09 11:48 出处:网络
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? 开发者_开发知识库

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消