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. URLurl=newURL(http://www.163.com);
  5. HttpURLConnectionhttpConn=(HttpURLConnection)url.openConnection();
  6. HttpURLConnection.setFollowRedirects(true);
  7. httpConn.setRequestMethod("GET");
  8. httpConn.setRequestProperty("User-Agent","Mozilla/4.0(compatible;MSIE6.0;Windows2000)");
  9. InputStreamin=httpConn.getInputStream();
  10. BufferedReadermyBufferedReader=newBufferedReader(newInputStreamReader(in));
  11. StringmyString=null;
  12. while((myString=myBufferedReader.readLine())!=null){
  13. System.out.println(myString);
  14. }

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

在使用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. URLurl=newURL(http://www.163.com);
  5. HttpURLConnectionhttpConn=(HttpURLConnection)url.openConnection();
  6. HttpURLConnection.setFollowRedirects(true);
  7. httpConn.setRequestMethod("GET");
  8. httpConn.setRequestProperty("User-Agent","Mozilla/4.0(compatible;MSIE6.0;Windows2000)");
  9. InputStreamin=httpConn.getInputStream();
  10. BufferedReadermyBufferedReader=newBufferedReader(newInputStreamReader(in));
  11. StringmyString=null;
  12. while((myString=myBufferedReader.readLine())!=null){
  13. System.out.println(myString);
  14. }

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

你可能感兴趣的:(java)