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

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();
 }
}

相关文章:

  • 2021-12-17
  • 2022-12-23
  • 2021-08-15
  • 2022-12-23
  • 2022-02-13
  • 2021-11-10
  • 2021-08-11
猜你喜欢
  • 2021-10-03
  • 2021-05-07
  • 2022-02-22
  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
  • 2021-12-11
相关资源
相似解决方案