HTTP代理测试小程序

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

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

代理的ip来源于网上,例如http://www.proxycn.com/http.php,复制到一个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();



    }

}

猜你喜欢

转载自blog.csdn.net/KAMILLE/article/details/2603510
今日推荐