Java使用代理访问网络的方法

有些时候我们的网络不能直接连接到外网, 需要使用http或是https或是socket代理来连接到外网,这里是java使用代理连接到外网的一些方法.

方法一:

使用系统属性来完成代理设置,这种方法比较简单, 但是不能对单独的连接来设置代理: 

Properties properties=System.getProperties();

// 设置http访问要使用的代理服务器的地址     

properties.setProperty("http.proxyHost", "192.168.0.254");     

// 设置http访问要使用的代理服务器的端口     

properties.setProperty("http.proxyPort", "8080");     

// 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔     

properties.setProperty("http.nonProxyHosts","localhost|192.168.0.*");     

// 设置安全访问使用的代理服务器地址与端口     

// 它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问     

properties.setProperty("https.proxyHost", "192.168.0.254");     

properties.setProperty("https.proxyPort", "443");     

// 使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机     

properties.setProperty("ftp.proxyHost", "192.168.0.254");     

properties.setProperty("ftp.proxyPort", "2121");     

properties.setProperty("ftp.nonProxyHosts","localhost|192.168.0.*");     

// socks代理服务器的地址与端口     

properties.setProperty("socksProxyHost", "192.168.0.254");     

properties.setProperty("socksProxyPort", "8000");

// 设置登陆到代理服务器的用户名和密码     

Authenticator.setDefault(new Authenticator() {

@Override

protected PasswordAuthentication getPasswordAuthentication(){

// TODO Auto-generated method stub

return new PasswordAuthentication(userName,password.toCharArray());

}

});

方法二:使用Proxy来对每个连接实现代理, 这种方法只能在jdk 1.5以上的版本使用(包含jdk1.5),优点是可以单独的设置每个连接的代理, 缺点是设置比较麻烦:
 try {       

  //创建代理服务器           

  InetSocketAddress addr = newInetSocketAddress("192.168.0.254",8080);           

  Proxyproxy = new Proxy(Proxy.Type.HTTP, addr); // http代理           

  //如果我们知道代理server的名字,可以直接使用         

  URLConnection conn =url.openConnection(proxy);           

  InputStream in =conn.getInputStream(); 

  String s =IOUtils.toString(in);           

  System.out.println(s);       

  } catch(Exception e){           

  e.printStackTrace();       

 }   

}

猜你喜欢

转载自www.linuxidc.com/Linux/2016-07/133756.htm