隨筆 - 0, 文章 - 264, 評(píng)論 - 170, 引用 - 0
          數(shù)據(jù)加載中……

          HttpUrlConnection通過(guò)代理訪問(wèn)網(wǎng)絡(luò)(轉(zhuǎn):http://www.iteye.com/problems/38978)

          1. import java.io.IOException;   
          2. import java.io.InputStream;   
          3. import java.net.InetSocketAddress;   
          4. import java.net.MalformedURLException;   
          5. import java.net.Proxy;   
          6. import java.net.ProxySelector;   
          7. import java.net.SocketAddress;   
          8. import java.net.URI;   
          9. import java.net.URISyntaxException;   
          10. import java.net.URL;   
          11. import java.net.URLConnection;   
          12. import java.util.List;   
          13. import java.util.Properties;   
          14.   
          15. public class NetProxy   
          16. {   
          17. // 測(cè)試本地JVM的網(wǎng)絡(luò)缺省配置   
          18. public void setLocalProxy()   
          19. {   
          20. Properties prop = System.getProperties();   
          21. //設(shè)置http訪問(wèn)要使用的代理服務(wù)器的地址   
          22. prop.setProperty("http.proxyHost", "10.10.0.96");   
          23. //設(shè)置http訪問(wèn)要使用的代理服務(wù)器的端口   
          24. prop.setProperty("http.proxyPort", "8080");   
          25. //設(shè)置不需要通過(guò)代理服務(wù)器訪問(wèn)的主機(jī),可以使用*通配符,多個(gè)地址用|分隔   
          26. prop.setProperty("http.nonProxyHosts", "localhost|10.10.*");   
          27.   
          28. //設(shè)置安全訪問(wèn)使用的代理服務(wù)器地址與端口   
          29. //它沒(méi)有https.nonProxyHosts屬性,它按照http.nonProxyHosts 中設(shè)置的規(guī)則訪問(wèn)   
          30. prop.setProperty("https.proxyHost", "10.10.0.96");   
          31. prop.setProperty("https.proxyPort", "443");   
          32.   
          33. //使用ftp代理服務(wù)器的主機(jī)、端口以及不需要使用ftp代理服務(wù)器的主機(jī)   
          34. prop.setProperty("ftp.proxyHost", "10.10.0.96");   
          35. prop.setProperty("ftp.proxyPort", "2121");   
          36. prop.setProperty("ftp.nonProxyHosts", "localhost|10.10.*");   
          37.   
          38. //socks代理服務(wù)器的地址與端口   
          39. prop.setProperty("socksProxyHost", "10.10.0.96");   
          40. prop.setProperty("socksProxyPort", "1080");   
          41. }   
          42.   
          43. // 清除proxy設(shè)置   
          44. public void removeLocalProxy()   
          45. {   
          46. Properties prop = System.getProperties();   
          47. prop.remove("http.proxyHost");   
          48. prop.remove("http.proxyPort");   
          49. prop.remove("http.nonProxyHosts");   
          50.   
          51. prop.remove("https.proxyHost");   
          52. prop.remove("https.proxyPort");   
          53.   
          54. prop.remove("ftp.proxyHost");   
          55. prop.remove("ftp.proxyPort");   
          56. prop.remove("ftp.nonProxyHosts");   
          57.   
          58. prop.remove("socksProxyHost");   
          59. prop.remove("socksProxyPort");   
          60. }   
          61.   
          62. //   
          63.   
          64. // 測(cè)試http   
          65. public void showHttpProxy(Object... proxy)   
          66. {   
          67. URL url = null;   
          68. try   
          69. {   
          70. url = new URL("http://blog.csdn.com/smallnest");   
          71. }   
          72. catch (MalformedURLException e)   
          73. {   
          74. return;   
          75. }   
          76. try   
          77. {   
          78. URLConnection conn = null;   
          79. switch (proxy.length)   
          80. {   
          81. case 0:   
          82. conn = url.openConnection();   
          83. break;   
          84. case 1:   
          85. conn = url.openConnection((Proxy) proxy[0]);   
          86. break;   
          87. default:   
          88. break;   
          89. }   
          90.   
          91. if (conn == null)   
          92. return;   
          93.   
          94. conn.setConnectTimeout(3000); // 設(shè)置連接超時(shí)時(shí)間   
          95. InputStream in = conn.getInputStream();   
          96. byte[] b = new byte[1024];   
          97. try   
          98. {   
          99. while (in.read(b) > 0)   
          100. {   
          101. System.out.println(new String(b));   
          102. }   
          103. }   
          104. catch (IOException e1)   
          105. {   
          106. }   
          107. }   
          108. catch (IOException e1)   
          109. {   
          110. e1.printStackTrace();   
          111. }   
          112.   
          113. }   
          114.   
          115. // 測(cè)試ftp   
          116. public void showFtpProxy(Object... proxy)   
          117. {   
          118. URL url = null;   
          119. try   
          120. {   
          121. url = new URL("ftp://ftp.tsinghua.edu.cn");   
          122. }   
          123. catch (MalformedURLException e)   
          124. {   
          125. return;   
          126. }   
          127. try   
          128. {   
          129. URLConnection conn = null;   
          130. switch (proxy.length)   
          131. {   
          132. case 0:   
          133. conn = url.openConnection();   
          134. break;   
          135. case 1:   
          136. conn = url.openConnection((Proxy) proxy[0]);   
          137. break;   
          138. default:   
          139. break;   
          140. }   
          141.   
          142. if (conn == null)   
          143. return;   
          144.   
          145. conn.setConnectTimeout(3000); // 設(shè)置連接超時(shí)時(shí)間   
          146. InputStream in = conn.getInputStream();   
          147. byte[] b = new byte[1024];   
          148. try   
          149. {   
          150. while (in.read(b) > 0)   
          151. {   
          152. System.out.println(new String(b));   
          153. }   
          154. }   
          155. catch (IOException e1)   
          156. {   
          157. }   
          158. }   
          159. catch (IOException e1)   
          160. {   
          161. e1.printStackTrace();   
          162. }   
          163.   
          164. }   
          165.   
          166. // 得到一個(gè)proxy   
          167. public Proxy getProxy(Proxy.Type type, String host, int port)   
          168. {   
          169. SocketAddress addr = new InetSocketAddress(host,port);   
          170. Proxy typeProxy = new Proxy(type, addr);   
          171. return typeProxy;   
          172. }   
          173.   
          174. public static void main(String[] args)   
          175. {   
          176. NetProxy proxy = new NetProxy();   
          177. //測(cè)試代理服務(wù)器   
          178. proxy.setLocalProxy();   
          179. proxy.showHttpProxy();   
          180.   
          181. //下面兩行是清除系統(tǒng)屬性,而通過(guò)Proxy類指定代理服務(wù)器   
          182. // proxy.removeLocalProxy   
          183. //proxy.showHttpProxy(proxy.getProxy(Proxy.Type.SOCKS,"10.10.0.96",1080));   
          184.   
          185.   
          186. }   

          posted on 2011-10-10 17:52 小一敗涂地 閱讀(5955) 評(píng)論(0)  編輯  收藏 所屬分類: java語(yǔ)言相關(guān)android+移動(dòng)開發(fā)

          主站蜘蛛池模板: 石河子市| 阿拉善左旗| 临海市| 丹寨县| 定结县| 西和县| 道真| 浪卡子县| 泗洪县| 衡阳市| 育儿| 镇原县| 墨江| 阿城市| 津市市| 屯昌县| 星子县| 三明市| 淮安市| 乌鲁木齐县| 辉县市| 宁武县| 石棉县| 鄂州市| 呼和浩特市| 饶阳县| 咸丰县| 顺义区| 峡江县| 略阳县| 农安县| 南江县| 巍山| 余姚市| 射洪县| 元朗区| 荃湾区| 石楼县| 秦皇岛市| 兰考县| 宁阳县|