[转]javaWeb代理访问Url

suziwen 写道:

J2SE1.5也被称为J2SE5.0,在5.0以前的代理服务器设置挺不友好,现在,5.0提供了对通过代理服务器的更加灵活的控制,它可以对http,https,ftp,socks等分别设置,而且还可以设置不需要通过代理服务器的主机和地址。这和我们在IE、firefox中设置代理服务器类似。

  1.你可以在通过java -DXXXX=YYYY方式在程序启动时设置,你也可以在程序中将设置放入系统属性中,你也可以设置Proxy类,通过它来控制。

  2.可以通过ProxySelector来做自己的代理服务器的负载平衡等。

Java代码 复制代码
  1.   
  2.   import java.io.IOException;    
  3.   import java.io.InputStream;    
  4.   import java.net.InetSocketAddress;    
  5.   import java.net.MalformedURLException;    
  6.   import java.net.Proxy;    
  7.   import java.net.ProxySelector;    
  8.   import java.net.SocketAddress;    
  9.   import java.net.URI;    
  10.   import java.net.URISyntaxException;    
  11.   import java.net.URL;    
  12.   import java.net.URLConnection;    
  13.   import java.util.List;    
  14.   import java.util.Properties;    
  15.   
  16.   public class NetProxy    
  17.   {    
  18.   // 测试本地JVM的网络缺省配置    
  19.   public void setLocalProxy()    
  20.   {    
  21.   Properties prop = System.getProperties();    
  22.   //设置http访问要使用的代理服务器的地址    
  23.   prop.setProperty("http.proxyHost""10.10.0.96");    
  24.   //设置http访问要使用的代理服务器的端口    
  25.   prop.setProperty("http.proxyPort""8080");    
  26.   //设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔    
  27.   prop.setProperty("http.nonProxyHosts""localhost|10.10.*");    
  28.   
  29.   //设置安全访问使用的代理服务器地址与端口    
  30.   //它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问    
  31.   prop.setProperty("https.proxyHost""10.10.0.96");    
  32.   prop.setProperty("https.proxyPort""443");    
  33.   
  34.   //使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机    
  35.   prop.setProperty("ftp.proxyHost""10.10.0.96");    
  36.   prop.setProperty("ftp.proxyPort""2121");    
  37.   prop.setProperty("ftp.nonProxyHosts""localhost|10.10.*");    
  38.   
  39.   //socks代理服务器的地址与端口    
  40.   prop.setProperty("socksProxyHost""10.10.0.96");    
  41.   prop.setProperty("socksProxyPort""1080");    
  42.   }    
  43.   
  44.   // 清除proxy设置    
  45.   public void removeLocalProxy()    
  46.   {    
  47.   Properties prop = System.getProperties();    
  48.   prop.remove("http.proxyHost");    
  49.   prop.remove("http.proxyPort");    
  50.   prop.remove("http.nonProxyHosts");    
  51.   
  52.   prop.remove("https.proxyHost");    
  53.   prop.remove("https.proxyPort");    
  54.   
  55.   prop.remove("ftp.proxyHost");    
  56.   prop.remove("ftp.proxyPort");    
  57.   prop.remove("ftp.nonProxyHosts");    
  58.   
  59.   prop.remove("socksProxyHost");    
  60.   prop.remove("socksProxyPort");    
  61.   }    
  62.   
  63.   //    
  64.   
  65.   // 测试http    
  66.   public void showHttpProxy(Object... proxy)    
  67.   {    
  68.   URL url = null;    
  69.   try    
  70.   {    
  71.   url = new URL("http://blog.csdn.com/smallnest");    
  72.   }    
  73.   catch (MalformedURLException e)    
  74.   {    
  75.   return;    
  76.   }    
  77.   try    
  78.   {    
  79.   URLConnection conn = null;    
  80.   switch (proxy.length)    
  81.   {    
  82.   case 0:    
  83.   conn = url.openConnection();    
  84.   break;    
  85.   case 1:    
  86.   conn = url.openConnection((Proxy) proxy[0]);    
  87.   break;    
  88.   default:    
  89.   break;    
  90.   }    
  91.   
  92.   if (conn == null)    
  93.   return;    
  94.   
  95.   conn.setConnectTimeout(3000); // 设置连接超时时间    
  96.   InputStream in = conn.getInputStream();    
  97.   byte[] b = new byte[1024];    
  98.   try    
  99.   {    
  100.   while (in.read(b) > 0)    
  101.   {    
  102.   System.out.println(new String(b));    
  103.   }    
  104.   }    
  105.   catch (IOException e1)    
  106.   {    
  107.   }    
  108.   }    
  109.   catch (IOException e1)    
  110.   {    
  111.   e1.printStackTrace();    
  112.   }    
  113.   
  114.   }    
  115.   
  116.   // 测试ftp    
  117.   public void showFtpProxy(Object... proxy)    
  118.   {    
  119.   URL url = null;    
  120.   try    
  121.   {    
  122.   url = new URL("ftp://ftp.tsinghua.edu.cn");    
  123.   }    
  124.   catch (MalformedURLException e)    
  125.   {    
  126.   return;    
  127.   }    
  128.   try    
  129.   {    
  130.   URLConnection conn = null;    
  131.   switch (proxy.length)    
  132.   {    
  133.   case 0:    
  134.   conn = url.openConnection();    
  135.   break;    
  136.   case 1:    
  137.   conn = url.openConnection((Proxy) proxy[0]);    
  138.   break;    
  139.   default:    
  140.   break;    
  141.   }    
  142.   
  143.   if (conn == null)    
  144.   return;    
  145.   
  146.   conn.setConnectTimeout(3000); // 设置连接超时时间    
  147.   InputStream in = conn.getInputStream();    
  148.   byte[] b = new byte[1024];    
  149.   try    
  150.   {    
  151.   while (in.read(b) > 0)    
  152.   {    
  153.   System.out.println(new String(b));    
  154.   }    
  155.   }    
  156.   catch (IOException e1)    
  157.   {    
  158.   }    
  159.   }    
  160.   catch (IOException e1)    
  161.   {    
  162.   e1.printStackTrace();    
  163.   }    
  164.   
  165.   }    
  166.   
  167.   // 得到一个proxy    
  168.   public Proxy getProxy(Proxy.Type type, String host, int port)    
  169.   {    
  170.   SocketAddress addr = new InetSocketAddress(host,port);    
  171.   Proxy typeProxy = new Proxy(type, addr);    
  172.   return typeProxy;    
  173.   }    
  174.   
  175.   public static void main(String[] args)    
  176.   {    
  177.   NetProxy proxy = new NetProxy();    
  178.   //测试代理服务器    
  179.   proxy.setLocalProxy();    
  180.   proxy.showHttpProxy();    
  181.   
  182.   //下面两行是清除系统属性,而通过Proxy类指定代理服务器    
  183.   // proxy.removeLocalProxy    
  184.   //proxy.showHttpProxy(proxy.getProxy(Proxy.Type.SOCKS,"10.10.0.96",1080));    
  185.   
  186.   
  187.   }    
  188.   }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值