Detect Proxy Settings for Internet Connection : Proxy « Network Protocol « Java






Detect Proxy Settings for Internet Connection

 

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 Main {
  public static void main(String[] args) throws Exception {
    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());
      }
    }
  }
}

   
  








Related examples in the same category

1.Identify ourself to a proxy
2.URL connection and proxy