java使用代理访问网络

在使用java的net包访问网络时,如果需要使用代理,可以这样做:

  1. System.getProperties().setProperty("proxySet""true");  
  2. System.getProperties().setProperty("http.proxyHost""183.121.23.188");  
  3. System.getProperties().setProperty("http.proxyPort""8080");  
  4.   
  5. URL url = new URL(http://www.163.com);  
  6. HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();  
  7. HttpURLConnection.setFollowRedirects(true);  
  8. httpConn.setRequestMethod("GET");  
  9. httpConn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");  
  10. InputStream in = httpConn.getInputStream();  
  11. BufferedReader myBufferedReader=new BufferedReader(new InputStreamReader(in));  
  12. String myString=null;  
  13. while((myString=myBufferedReader.readLine())!=null){  
  14.     System.out.println(myString);  
  15. }  

其中,前三行就是设置代理的语句。

原文地址:https://www.cnblogs.com/xhk1228/p/3194871.html