Java 示例 - 查找代理设置

  • 问题描述

    如何查找系统的代理设置?
  • 解决方案

    以下示例显示了如何使用 systemSetting 的 put 方法和 HttpURLConnection 类的 getResponse 方法在系统上查找代理设置和创建代理连接。
    
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.Properties;
    import java.net.InetSocketAddress;
    import java.net.Proxy;
    import java.net.ProxySelector;
    import java.net.URI;
    public class Main{
       public static void main(String s[]) throws Exception {
          try {
             Properties systemSettings = System.getProperties();
             systemSettings.put("proxySet", "true");
             systemSettings.put("http.proxyHost", "proxy.mycompany1.local");
             systemSettings.put("http.proxyPort", "80");
             
             URL u = new URL("http://www.google.com");
             HttpURLConnection con = (HttpURLConnection)u.openConnection();
             System.out.println(con.getResponseCode() + " : " + con.getResponseMessage());
             System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK);
          } catch (Exception e) {
             e.printStackTrace();
             System.out.println(false);
          }
          System.setProperty("java.net.useSystemProxies", "true");
          Proxy proxy = (Proxy) ProxySelector.getDefault().select(new URI(
             "http://www.yahoo.com/")).iterator().
          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());
          }
       }
    }
    
  • 结果

    上面的代码示例将产生以下结果。
    
    200 : OK
    true
    proxy hostname : HTTP
    proxy hostname : proxy.mycompany1.local
    proxy port : 80
    
    以下是查找系统代理设置的示例?
    
    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 NewClass {
       public static void main(String[] args) {
          try {
             System.setProperty("java.net.useSystemProxies", "true");
             List<Proxy> l = ProxySelector.getDefault().select(
                new URI("http://www.google.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();
          }
       }
    }
    
    上面的代码示例将产生以下结果。
    
    proxy hostname : DIRECT
    No Proxy