|
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.InetSocketAddress;
- import java.net.MalformedURLException;
- import java.net.Proxy;
- import java.net.ProxySelector;
- import java.net.SocketAddress;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.List;
- import java.util.Properties;
-
- public class NetProxy
- {
- // 測試本地JVM的網絡缺省配置
- public void setLocalProxy()
- {
- Properties prop = System.getProperties();
- //設置http訪問要使用的代理服務器的地址
- prop.setProperty("http.proxyHost", "10.10.0.96");
- //設置http訪問要使用的代理服務器的端口
- prop.setProperty("http.proxyPort", "8080");
- //設置不需要通過代理服務器訪問的主機,可以使用*通配符,多個地址用|分隔
- prop.setProperty("http.nonProxyHosts", "localhost|10.10.*");
-
- //設置安全訪問使用的代理服務器地址與端口
- //它沒有https.nonProxyHosts屬性,它按照http.nonProxyHosts 中設置的規則訪問
- prop.setProperty("https.proxyHost", "10.10.0.96");
- prop.setProperty("https.proxyPort", "443");
-
- //使用ftp代理服務器的主機、端口以及不需要使用ftp代理服務器的主機
- prop.setProperty("ftp.proxyHost", "10.10.0.96");
- prop.setProperty("ftp.proxyPort", "2121");
- prop.setProperty("ftp.nonProxyHosts", "localhost|10.10.*");
-
- //socks代理服務器的地址與端口
- prop.setProperty("socksProxyHost", "10.10.0.96");
- prop.setProperty("socksProxyPort", "1080");
- }
-
- // 清除proxy設置
- public void removeLocalProxy()
- {
- Properties prop = System.getProperties();
- prop.remove("http.proxyHost");
- prop.remove("http.proxyPort");
- prop.remove("http.nonProxyHosts");
-
- prop.remove("https.proxyHost");
- prop.remove("https.proxyPort");
-
- prop.remove("ftp.proxyHost");
- prop.remove("ftp.proxyPort");
- prop.remove("ftp.nonProxyHosts");
-
- prop.remove("socksProxyHost");
- prop.remove("socksProxyPort");
- }
-
- //
-
- // 測試http
- public void showHttpProxy(Object... proxy)
- {
- URL url = null;
- try
- {
- url = new URL("http://blog.csdn.com/smallnest");
- }
- catch (MalformedURLException e)
- {
- return;
- }
- try
- {
- URLConnection conn = null;
- switch (proxy.length)
- {
- case 0:
- conn = url.openConnection();
- break;
- case 1:
- conn = url.openConnection((Proxy) proxy[0]);
- break;
- default:
- break;
- }
-
- if (conn == null)
- return;
-
- conn.setConnectTimeout(3000); // 設置連接超時時間
- InputStream in = conn.getInputStream();
- byte[] b = new byte[1024];
- try
- {
- while (in.read(b) > 0)
- {
- System.out.println(new String(b));
- }
- }
- catch (IOException e1)
- {
- }
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
-
- }
-
- // 測試ftp
- public void showFtpProxy(Object... proxy)
- {
- URL url = null;
- try
- {
- url = new URL("ftp://ftp.tsinghua.edu.cn");
- }
- catch (MalformedURLException e)
- {
- return;
- }
- try
- {
- URLConnection conn = null;
- switch (proxy.length)
- {
- case 0:
- conn = url.openConnection();
- break;
- case 1:
- conn = url.openConnection((Proxy) proxy[0]);
- break;
- default:
- break;
- }
-
- if (conn == null)
- return;
-
- conn.setConnectTimeout(3000); // 設置連接超時時間
- InputStream in = conn.getInputStream();
- byte[] b = new byte[1024];
- try
- {
- while (in.read(b) > 0)
- {
- System.out.println(new String(b));
- }
- }
- catch (IOException e1)
- {
- }
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
-
- }
-
- // 得到一個proxy
- public Proxy getProxy(Proxy.Type type, String host, int port)
- {
- SocketAddress addr = new InetSocketAddress(host,port);
- Proxy typeProxy = new Proxy(type, addr);
- return typeProxy;
- }
-
- public static void main(String[] args)
- {
- NetProxy proxy = new NetProxy();
- //測試代理服務器
- proxy.setLocalProxy();
- proxy.showHttpProxy();
-
- //下面兩行是清除系統屬性,而通過Proxy類指定代理服務器
- // proxy.removeLocalProxy
- //proxy.showHttpProxy(proxy.getProxy(Proxy.Type.SOCKS,"10.10.0.96",1080));
-
-
- }
- }
|