2

我有一大堆代理服务器(txt 文件,每行中的 Format = 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);
这对于一个大名单来说并不好。
我是按照正确的方向编写这些代码还是应该更改它们(哪些部分)?
我怎样才能优化它们?

提前致谢

4

2 回答 2

6

有很多事情可以改进。

  • 半秒内不要让线程休眠。
  • 放弃 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);
}
于 2012-09-03T14:31:54.603 回答
1

我可能会做这样的事情:

public static bool TestProxy(string ipAddress, int port, out string errorMsg, out double connectionSeconds) {
  Stopwatch stopWatch = new Stopwatch();
  errorMsg = "";
  connectionSeconds = -1;

  try {
    stopWatch.Start();
    var client = new RestClient("https://webapi.theproxisright.com/");
    client.Proxy = new WebProxy(ipAddress, port);

    var request = new RestRequest("api/ip", Method.GET);
    request.Timeout = 10000;
    request.RequestFormat = DataFormat.Json;

    var response = client.Execute(request);
    if (response.ErrorException != null) {
      throw response.ErrorException;
    }
    return (response.Content == ipAddress);
  } catch (Exception ex) {
    errorMsg = ex.Message;
    return false;
  } finally {
    stopWatch.Stop();
    connectionSeconds = stopWatch.Elapsed.TotalSeconds;
  }
}

使用类似 WhatIsMyIP 的 REST 服务(我使用来自https://TheProxIsRight.com的服务)。

然后如上所述,我可能会尝试将它与类似的东西并行化:

  Task.Factory.StartNew(() => {
    try {
      string errorMsg;
      double connectionTime;
      var success = TestProxy("1.2.3.4",3128, out errorMsg, out connectionTime);

      //Log Result
    } catch (Exception ex) {
      //Log Error
    }
  });

请注意,也可以使用上述站点上的 REST API 来查询工作代理: https ://theproxisright.com/#apidemo

(披露,我在上述网站工作)

于 2014-05-09T07:18:09.463 回答