URL如何通过Proxy代理访问Internet资源

如果运行时的环境是通过代理服务器来上网,此时需要设置代理服务器之后才能正确访问网路资源

public static void main(String[] args){
 String url = "http://java.sun.com/index.html";
 //获取系统属性集合
 Properties prop = System.getProperties();
 //设置代理服务器的地址
 prop.put("http.proxyHost","172.16.0.10");
 //设置代理服务器端口
 prop.put("http.proxyPort","11080");
 try{
  URL url = new URL(url);
  URLConnection conn = url.openConnection();
  conn.connect();
  //获取网络资源的大小
  System.out.println(conn.getCnntentLength());
  //获取网路资源的输入流取信息
  InputStream is  = conn.getInputStream();
  System.out.println("ic:"+ is.read());
  is.close();
 }catch(Exception e){
  e.printStackTrace();
 }
}

原文地址:https://www.cnblogs.com/gaoxiang116/p/3654377.html