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

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

public static void MyChecker() { string[] lines = File.ReadAllLines(txtProxyListPath.Text); List list_lines = new List(lines); List list_lines_RemovedDup = new List(); HashSet HS = new HashSet(); 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但仍在工作)
  • 使用仅获取HEAD的HttpWebRequest替换DownloadString。
  • 将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); } 

我可能会这样做:

 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

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