码迷,mamicode.com
首页 > 编程语言 > 详细

Java如何查找系统的代理设置?

时间:2018-09-10 11:11:27      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:exce   toolbar   ddr   系统   class   lis   soc   pack   查找   

在Java编程中,如何查找系统的代理设置?

以下示例显示如何使用HttpURLConnection类的systemSetting()方法和getResponse()方法的put方法在系统上查找代理设置并创建代理连接。

package com.yiibai;

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 FindProxySettings{
   public static void main(String s[]) throws Exception {
      try {
         Properties systemSettings = System.getProperties();
         systemSettings.put("proxySet", "true");
         systemSettings.put("http.proxyHost", "proxy.yiibai.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.baidu.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());
      }
   }
}
Java

上述代码示例将产生以下结果 -

200 : OK
true
proxy hostname : HTTP
proxy hostname : proxy.yiibai.local
proxy port : 80
Shell

示例-2

以下是查找系统的代理设置的另一个示例:

package com.yiibai;

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

上述代码示例将产生以下结果 -

proxy hostname : DIRECT
No Proxy

Java如何查找系统的代理设置?

标签:exce   toolbar   ddr   系统   class   lis   soc   pack   查找   

原文地址:https://www.cnblogs.com/borter/p/9617178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!