java的各种代理设置

分类: java_socket 24人阅读 评论(0) 收藏 举报

有些时候我们的网络不能直接连接到外网, 需要使用http或是https或是socket代理来连接到外网, 这里是java使用代理连接到外网的一些方法,:方法一使用系统属性来完成代理设置, 这种方法比较简单, 但是不能对单独的连接来设置代理:

  1. public static void main(String[] args) {  
  2.     Properties prop = System.getProperties();  
  3.     // 设置http访问要使用的代理服务器的地址  
  4.     prop.setProperty("http.proxyHost""192.168.0.254");  
  5.     // 设置http访问要使用的代理服务器的端口  
  6.     prop.setProperty("http.proxyPort""8080");  
  7.     // 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔  
  8.     prop.setProperty("http.nonProxyHosts""localhost|192.168.0.*");  
  9.     // 设置安全访问使用的代理服务器地址与端口  
  10.     // 它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问  
  11.     prop.setProperty("https.proxyHost""192.168.0.254");  
  12.     prop.setProperty("https.proxyPort""443");  
  13.     // 使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机  
  14.     prop.setProperty("ftp.proxyHost""192.168.0.254");  
  15.     prop.setProperty("ftp.proxyPort""2121");  
  16.     prop.setProperty("ftp.nonProxyHosts""localhost|192.168.0.*");  
  17.     // socks代理服务器的地址与端口  
  18.     prop.setProperty("socksProxyHost""192.168.0.254");  
  19.     prop.setProperty("socksProxyPort""8000");  
  20.     // 设置登陆到代理服务器的用户名和密码  
  21.     Authenticator.setDefault(new MyAuthenticator("userName""Password"));  
  22. }  
  23. static class MyAuthenticator extends Authenticator {  
  24.     private String user = "";  
  25.     private String password = "";  
  26.     public MyAuthenticator(String user, String password) {  
  27.         this.user = user;  
  28.         this.password = password;  
  29.     }  
  30.     protected PasswordAuthentication getPasswordAuthentication() {  
  31.         return new PasswordAuthentication(user, password.toCharArray());  
  32.     }  
  33. }  
  1. public static void main(String[] args) {  
  2.     Properties prop = System.getProperties();  
  3.     // 设置http访问要使用的代理服务器的地址   
  4.     prop.setProperty("http.proxyHost""192.168.0.254");  
  5.     // 设置http访问要使用的代理服务器的端口   
  6.     prop.setProperty("http.proxyPort""8080");  
  7.     // 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔   
  8.     prop.setProperty("http.nonProxyHosts""localhost|192.168.0.*");  
  9.     // 设置安全访问使用的代理服务器地址与端口   
  10.     // 它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问   
  11.     prop.setProperty("https.proxyHost""192.168.0.254");  
  12.     prop.setProperty("https.proxyPort""443");  
  13.     // 使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机   
  14.     prop.setProperty("ftp.proxyHost""192.168.0.254");  
  15.     prop.setProperty("ftp.proxyPort""2121");  
  16.     prop.setProperty("ftp.nonProxyHosts""localhost|192.168.0.*");  
  17.     // socks代理服务器的地址与端口   
  18.     prop.setProperty("socksProxyHost""192.168.0.254");  
  19.     prop.setProperty("socksProxyPort""8000");  
  20.     // 设置登陆到代理服务器的用户名和密码   
  21.     Authenticator.setDefault(new MyAuthenticator("userName""Password"));  
  22. }  
  23. static class MyAuthenticator extends Authenticator {  
  24.     private String user = "";  
  25.     private String password = "";  
  26.     public MyAuthenticator(String user, String password) {  
  27.         this.user = user;  
  28.         this.password = password;  
  29.     }  
  30.     protected PasswordAuthentication getPasswordAuthentication() {  
  31.         return new PasswordAuthentication(user, password.toCharArray());  
  32.     }  
  33. }  
 
方法二 使用 Proxy 来对每个连接实现代理 ,  这种方法只能在 jdk 1.5 以上的版本使用 ( 包含 jdk1.5),  优点是可以单独的设置每个连接的代理 ,  缺点是设置比较麻烦 :
  1. public static void main(String[] args) {  
  2.     try {  
  3.         URL url = new URL("http://www.baidu.com");  
  4.         // 创建代理服务器  
  5.         InetSocketAddress addr = new InetSocketAddress("192.168.0.254",  
  6.                 8080);  
  7.         // Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // Socket 代理  
  8.         Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理  
  9.         // 如果我们知道代理server的名字, 可以直接使用  
  10.         // 结束  
  11.         URLConnection conn = url.openConnection(proxy);  
  12.         InputStream in = conn.getInputStream();  
  13.         // InputStream in = url.openStream();  
  14.         String s = IOUtils.toString(in);  
  15.         System.out.println(s);  
  16.     } catch (Exception e) {  
  17.         e.printStackTrace();  
  18.     }  
  19. }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值