隨筆 - 0, 文章 - 264, 評論 - 170, 引用 - 0
          數據加載中……

          HttpUrlConnection通過代理訪問網絡(轉: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. // 測試本地JVM的網絡缺省配置   
          18. public void setLocalProxy()   
          19. {   
          20. Properties prop = System.getProperties();   
          21. //設置http訪問要使用的代理服務器的地址   
          22. prop.setProperty("http.proxyHost", "10.10.0.96");   
          23. //設置http訪問要使用的代理服務器的端口   
          24. prop.setProperty("http.proxyPort", "8080");   
          25. //設置不需要通過代理服務器訪問的主機,可以使用*通配符,多個地址用|分隔   
          26. prop.setProperty("http.nonProxyHosts", "localhost|10.10.*");   
          27.   
          28. //設置安全訪問使用的代理服務器地址與端口   
          29. //它沒有https.nonProxyHosts屬性,它按照http.nonProxyHosts 中設置的規則訪問   
          30. prop.setProperty("https.proxyHost", "10.10.0.96");   
          31. prop.setProperty("https.proxyPort", "443");   
          32.   
          33. //使用ftp代理服務器的主機、端口以及不需要使用ftp代理服務器的主機   
          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代理服務器的地址與端口   
          39. prop.setProperty("socksProxyHost", "10.10.0.96");   
          40. prop.setProperty("socksProxyPort", "1080");   
          41. }   
          42.   
          43. // 清除proxy設置   
          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. // 測試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); // 設置連接超時時間   
          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. // 測試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); // 設置連接超時時間   
          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. // 得到一個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. //測試代理服務器   
          178. proxy.setLocalProxy();   
          179. proxy.showHttpProxy();   
          180.   
          181. //下面兩行是清除系統屬性,而通過Proxy類指定代理服務器   
          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 小一敗涂地 閱讀(5951) 評論(0)  編輯  收藏 所屬分類: java語言相關android+移動開發

          主站蜘蛛池模板: 桂平市| 台东市| 和政县| 盐城市| 渝中区| 新化县| 临邑县| 普兰县| 泸州市| 黄龙县| 拉萨市| 宕昌县| 灵武市| 玉树县| 屯昌县| 阳朔县| 鄄城县| 蒙城县| 乌鲁木齐县| 平乐县| 大安市| 于都县| 当涂县| 射阳县| 保靖县| 衡水市| 炉霍县| 乐清市| 马龙县| 吉安市| 贺州市| 焉耆| 丹棱县| 莱芜市| 临湘市| 玛纳斯县| 临江市| 塔河县| 闽侯县| 扎鲁特旗| 灵宝市|