c# - 检查特定网站上的大代理列表的最快方法是什么?

标签 c# .net-4.0 proxy windows-applications

我有一个很大的代理服务器列表(txt 文件,每行格式 = ip:port)并编写了下面的代码来检查它们:

    public static void MyChecker()
    {
        string[] lines = File.ReadAllLines(txtProxyListPath.Text);
        List<string> list_lines = new List<string>(lines);
        List<string> list_lines_RemovedDup = new List<string>();
        HashSet<string> HS = new HashSet<string>();
        int Duplicate_Count = 0;
        int badProxy = 0;
        int CheckedCount = 0;

        foreach (string line in list_lines)
        {
            string[] line_char = line.Split(':');
            string ip = line_char[0];
            string port = line_char[1];
            if (CanPing(ip))
            {
                if (SoketConnect(ip, port))
                {
                    if (CheckProxy(ip, port))
                    {
                        string ipAndport = ip + ":" + port;
                        if (HS.Add(ipAndport))
                        {
                            list_lines_RemovedDup.Add(ipAndport);
                            CheckedCount++;
                        }
                        else
                        {
                            Duplicate_Count++;
                            CheckedCount++;
                        }
                    }
                    else
                    {
                        badProxy++;
                        CheckedCount++;
                    }
                }
                else
                {
                    badProxy++;
                    CheckedCount++;
                }
            }
            else
            {
                badProxy++;
                CheckedCount++;
            }
    }

    public static bool CanPing(string ip)
    {
        Ping ping = new Ping();

        try
        {
            PingReply reply = ping.Send(ip, 2000);
            if (reply == null)
                return false;

            return (reply.Status == IPStatus.Success);
        }
        catch (PingException Ex)
        {
            return false;
        }
    }

    public static bool SoketConnect(string ip, string port)
    {
        var is_success = false;
        try
        {
            var connsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            connsock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 200);
            System.Threading.Thread.Sleep(500);
            var hip = IPAddress.Parse(ip);
            var ipep = new IPEndPoint(hip, int.Parse(port));
            connsock.Connect(ipep);
            if (connsock.Connected)
            {
                is_success = true;
            }
            connsock.Close();
        }
        catch (Exception)
        {
            is_success = false;
        }
        return is_success;
    }

    public static bool CheckProxy(string ip, string port)
    {
        try
        {
            WebClient WC = new WebClient();
            WC.Proxy = new WebProxy(ip, int.Parse(port));
            WC.DownloadString("http://SpecificWebSite.com");
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

但我认为我应该重写这些代码,因为它们非常慢。
我在这些线路上有严重的延误:
WC.DownloadString("http://SpecificWebSite.com");

PingReply reply = ping.Send(ip, 2000);
这对于大列表来说并不好。
我是否按照正确的方向编写了这些代码,或者我应该更改它们(哪些部分)?
我该如何优化它们?

提前致谢

最佳答案

您可以改进很多事情。

  • 不要让线程休眠半秒钟。
  • 放弃 ping 检查(因为代理可能在防火墙后面并且 不响应 ping 但仍在工作)
  • 将 DownloadString 替换为仅获取 HEAD 的 HttpWebRequest。
  • 将 HttpWebRequest 的超时设置为低于 默认(无需等待那么久。如果代理在 10-20 秒,那么您可能不想使用它)。
  • 将您的大列表拆分成较小的列表并同时处理它们 时间。

仅这些就可以大大加快您的流程。

根据要求,这里有一个如何使用 HttpWebRequests 的例子

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = null;   // set proxy here
request.Timeout = 10000; 
request.Method = "HEAD";

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    Console.WriteLine(response.StatusCode);
}

关于c# - 检查特定网站上的大代理列表的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12249702/

相关文章:

c# - 为什么这个通用扩展方法不能编译?

apache - 使用 apache web 服务器代理到 apache tomcat

java - Netty 代理复制流量

c# - Windows Phone 8.1 将文本复制到剪贴板

c# - 将字符串值设置为属性类型/XML 反序列化到类。 C#

c# - Mono C# 的 CSS 样式

c# - XNA 4.0 中的 2D BoundingRectangle 旋转

.net - ASMX 操作 404s,但 ASMX 服务描述没有,url 路由问题?

c# - 很奇怪的 C# RemotingException

linux - 我如何配置 fluentd 代理来安装插件?