Tuesday, October 23, 2007

How to detect Proxy Settings for Internet Connection

ava 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:

public class testProxy {

public static void main(String[] args) {
try {

System.setProperty("java.net.useSystemProxies","true");
List l = ProxySelector.getDefault().select(
new URI("http://www.yahoo.com/"));

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

No comments: