HttpUrlConnection通過(guò)代理訪問(wèn)網(wǎng)絡(luò)(轉(zhuǎn):http://www.iteye.com/problems/38978)
- 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
- {
- // 測(cè)試本地JVM的網(wǎng)絡(luò)缺省配置
- public void setLocalProxy()
- {
- Properties prop = System.getProperties();
- //設(shè)置http訪問(wèn)要使用的代理服務(wù)器的地址
- prop.setProperty("http.proxyHost", "10.10.0.96");
- //設(shè)置http訪問(wèn)要使用的代理服務(wù)器的端口
- prop.setProperty("http.proxyPort", "8080");
- //設(shè)置不需要通過(guò)代理服務(wù)器訪問(wèn)的主機(jī),可以使用*通配符,多個(gè)地址用|分隔
- prop.setProperty("http.nonProxyHosts", "localhost|10.10.*");
- //設(shè)置安全訪問(wèn)使用的代理服務(wù)器地址與端口
- //它沒(méi)有https.nonProxyHosts屬性,它按照http.nonProxyHosts 中設(shè)置的規(guī)則訪問(wèn)
- prop.setProperty("https.proxyHost", "10.10.0.96");
- prop.setProperty("https.proxyPort", "443");
- //使用ftp代理服務(wù)器的主機(jī)、端口以及不需要使用ftp代理服務(wù)器的主機(jī)
- prop.setProperty("ftp.proxyHost", "10.10.0.96");
- prop.setProperty("ftp.proxyPort", "2121");
- prop.setProperty("ftp.nonProxyHosts", "localhost|10.10.*");
- //socks代理服務(wù)器的地址與端口
- prop.setProperty("socksProxyHost", "10.10.0.96");
- prop.setProperty("socksProxyPort", "1080");
- }
- // 清除proxy設(shè)置
- 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");
- }
- //
- // 測(cè)試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); // 設(shè)置連接超時(shí)時(shí)間
- 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();
- }
- }
- // 測(cè)試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); // 設(shè)置連接超時(shí)時(shí)間
- 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();
- }
- }
- // 得到一個(gè)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();
- //測(cè)試代理服務(wù)器
- proxy.setLocalProxy();
- proxy.showHttpProxy();
- //下面兩行是清除系統(tǒng)屬性,而通過(guò)Proxy類指定代理服務(wù)器
- // proxy.removeLocalProxy
- //proxy.showHttpProxy(proxy.getProxy(Proxy.Type.SOCKS,"10.10.0.96",1080));
- }
- }
posted on 2011-10-10 17:52 小一敗涂地 閱讀(5955) 評(píng)論(0) 編輯 收藏 所屬分類: java語(yǔ)言相關(guān) 、android+移動(dòng)開發(fā)