搜索使用代理

本文介绍了一个Java程序,用于测试HTTP代理的有效性。通过解析文本文件中的代理信息,并使用这些代理尝试访问百度网站来验证代理是否可用。

package com.xyj.proxy;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
*
@author Tower
*
@version Jun 3, 2008 2:40:54 PM
*/
public class TestProxy {

   
public void setLocalProxy() {
        Properties prop
= System.getProperties();
        prop.setProperty(
"http.proxyHost", "222.222.222.222");
        prop.setProperty(
"http.proxyPort", "3128");

    }
   
// 清除proxy设置
    public void removeLocalProxy()
    {
    Properties prop
= System.getProperties();
    prop.remove(
"http.proxyHost");
    prop.remove(
"http.proxyPort");
    prop.remove(
"http.nonProxyHosts");
    prop.remove(
"https.proxyHost");
    prop.remove(
"https.proxyPort");
    prop.remove(
"ftp.proxyHost");
    prop.remove(
"ftp.proxyPort");
    prop.remove(
"ftp.nonProxyHosts");
    prop.remove(
"socksProxyHost");
    prop.remove(
"socksProxyPort");
    }
   
// 测试HTTP代理
   
//public void showHttpProxy(Object... proxy) {
    public void showHttpProxy(Object ... proxy) {
       
        URL url
= null;
       
try {
            url
= new URL("http://www.baidu.com");
        }
catch (MalformedURLException e) {
           
return;
        }
       
try {
            URLConnection conn
= null;
           
switch (proxy.length) {
           
case 0:
                conn
= url.openConnection();
               
break;
           
case 1:
                conn
= url.openConnection((Proxy) proxy[0]);
               
break;
           
default:
               
break;
            }
           
if (conn == null)
               
return;
            conn.setConnectTimeout(
10000); // 设置连接超时时间
            InputStream in = conn.getInputStream();
            BufferedReader br
= new BufferedReader(new java.io.InputStreamReader(in));
            String str
= null;
           
try {
               
while ((str = br.readLine())!=null) {
                   
if(str.indexOf("百度一下,你就知道")>0){
                       
break;
                    }
                }
           
            }
catch (IOException e1) {
            }
        }
catch (IOException e1) {
           
return;
        }
    }

   
// 得到一个proxy
    public static Proxy getProxy(String host, int port) {
        SocketAddress addr
= new InetSocketAddress(host, port);
        Proxy typeProxy
= new Proxy(Proxy.Type.HTTP, addr);
       
return typeProxy;
    }
/*    // 得到一个proxy
    public Proxy getProxy(Proxy.Type type, String host, int port) {
        SocketAddress addr = new InetSocketAddress(host, port);
        Proxy typeProxy = new Proxy(type, addr);
        return typeProxy;
    }
*/
   
public void test(){
        String pathurl
= "d:/temp.txt";
       
try {
            File file
= new File(pathurl);
            FileInputStream fin
= new FileInputStream(file);
            BufferedInputStream bin
= new BufferedInputStream(fin);
            BufferedReader ir
= new BufferedReader(new InputStreamReader(bin));
            String str
= null;
           
int count = 0;
            StringBuffer ip
= new StringBuffer();
           
while ((str = ir.readLine()) != null) {
           
                String ipfilter
= "//d{1,3}//.//d{1,3}//.//d{1,3}//.//d{1,3}";

                Pattern p
= Pattern.compile(ipfilter);
                Matcher m
= p.matcher(str);
               
if (m.find()) {
                    count
++;
                    String port
= str.substring(str.indexOf(":")+1,str.length());
                   
if(port.indexOf("0")==0){
                        port
= port.substring(2, port.length());
                    }
                    String host
= m.group();
                   
int po = Integer.parseInt(port);
                   
//System.out.println(host+"--"+port);
                    System.out.println("----------"+count);
                    showHttpProxy(getProxy(host,po));
                }
               
// 获取端口号

            }
       
/*    System.out.println(ip);
            java.io.FileWriter fileWriter = new java.io.FileWriter(pathurl);
            fileWriter.write(ip.toString());
            fileWriter.close();
            bin.close();
            fin.close();
*/
        }
catch (Exception e) {

        }
       
//showHttpProxy(getProxy("58.211.78.143",80));
    }
   
public static void main(String[] args) {
        TestProxy proxy
= new TestProxy();
       
//proxy.setLocalProxy();
        proxy.showHttpProxy(getProxy("203.162.2.138",80));
       
       
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值