HTTP代理测试一段代码

上网不顺畅怎么办了,不知道为什么最近国内不能上sourceforge.net,不能上,上网都没意义了

想到找代理来上,不过发现很多代理都不能用,逐个copy到浏览器测试,太累了,想到写一个小程序来测试

代理的ip来源于网上,例如http://www.uu-ip.com/,复制到一个txt上面,然后用小程序批出来测试,可以连同的,把结果写到pass.txt上面

程序是多线程的,所以速度很快,同时开他100个。

using System;

using System.Collections;

using System.IO;

using System.Net;

using System.Threading;

public class HttpProxyRequest

{

public static string GetHttpString(string proxyAddress)

{

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.126.com");

    request.Timeout = 10000;



    if (proxyAddress != null && proxyAddress != string.Empty)

    {

        string ip;

        int port = 80;

        if (proxyAddress.IndexOf(':') == -1)

        {

            ip = proxyAddress;

        }

        else

        {

            string[] array = proxyAddress.Split(':');

            ip = array[0];

            try

            {

                port = int.Parse(array[1]);

            }

            catch

            {

                return string.Empty;

            }

        }

        try

        {

            WebProxy proxy = new WebProxy(ip, port);

            request.Proxy = proxy;

        }

        catch (Exception err)

        {

            Console.Write("错误:" + err.Message);

            return string.Empty;

        }

    }



    try

    {

        WebResponse response = request.GetResponse();

        Stream stream = response.GetResponseStream();

        StreamReader reader = new StreamReader(stream);

        string result = reader.ReadToEnd();

        reader.Close();

        return result;

    }

    catch (Exception err)

    {

        Console.WriteLine("错误:" + err.Message);

        return string.Empty;

    }

}

}

public class test

{

static string original;



private static object sync = new object();

private static int count;

public static int Count

{

    get { lock (sync) { return count; } }

    set { lock (sync) { count = value; } }

}



private static int index;

public static int Index

{

    get { lock (sync) { return index; } }

    set { lock (sync) { index = value; } }

}



static ArrayList ipList = new ArrayList();

static ArrayList passList = new ArrayList();



static void RunInThread()

{

    Count++;

    string ip = ipList[Index].ToString();

    Index++;

    string result = HttpProxyRequest.GetHttpString(ip);

    if (original == result)

    {

        Console.WriteLine("{0}符合!!!!!!!!!!!!", ip);

        passList.Add(ip);

    }

    else

        Console.WriteLine("{0}不符合!", ip);

    Count--;

}



static void Main(string[] args)

{

    original = HttpProxyRequest.GetHttpString("");



    ipList = new ArrayList();

    StreamReader reader = new StreamReader("C://proxy.txt");

    string line;

    while ((line = reader.ReadLine()) != null)

    {

        try

        {

            //string[] array = line.Split('/t');

            //string ip = array[0];



            string[] array = line.Split(' ');

            string ip = array[1] + ":" + array[2];

            ipList.Add(ip);

        }

        catch (Exception err)

        {

            Console.WriteLine(err.Message);

        }

    }

    reader.Close();



    while (Count < 100 && Index < ipList.Count)

    {

        new Thread(new ThreadStart(RunInThread)).Start();

        System.Threading.Thread.Sleep(100);

    }

    while (Count > 0)

    {

        System.Threading.Thread.Sleep(100);

    }

    if (passList.Count > 0)

    {

        StreamWriter writer = File.CreateText("C://pass.txt");

        foreach (string s in passList)

            writer.WriteLine(s);

        writer.Close();

    }

    Console.WriteLine("完成");

    Console.ReadLine();



}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值