Connection在代理服务器的应用

  • import java.io.IOException;  
  • import java.io.InputStream;  
  • import java.net.InetSocketAddress;  
  • import java.net.Proxy;  
  • import java.net.SocketAddress;  
  • import java.net.URL;  
  • import java.net.URLConnection;  
  • import java.util.Properties;  
  •    
  •    
  • public class Test {  
  •     /** 
  •      * @param args 
  •      * @throws IOException  
  •      */  
  •     public static void main(String[] args) throws IOException {  
  •         //Properties prop = System.getProperties();  
  •         //设置http访问要使用的代理服务器的地址  
  •         //prop.setProperty("http.proxyHost", "192.9.208.16");  
  •         //设置http访问要使用的代理服务器的端口  
  •         //prop.setProperty("http.proxyPort", "3128");  
  •         //设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔  
  •         //prop.setProperty("http.nonProxyHosts", "localhost|192.9.*");  
  •    
  •         URL url = new URL("http://9mmo.com");  
  •         SocketAddress addr = new InetSocketAddress("192.9.208.15",80);//是代理地址:192.9.208.16:3128  
  •         Proxy typeProxy = new Proxy(Proxy.Type.HTTP, addr);  
  •         URLConnection conn = url.openConnection(typeProxy);  
  •         if (conn == null)  
  •             return;  
  •    
  •         conn.setConnectTimeout(3000); // 设置连接超时时间  
  •         InputStream in = conn.getInputStream();  
  •         byte[] b = new byte[1024];  
  •         while (in.read(b) > 0)  
  •         {  
  •             System.out.println(new String(b));  
  •         }  
  •     }  
  •    
  • }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值