用java实现代理

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值