What is the fastest way for checking a big proxy list on a specific web site?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 11:56:14

There are quite a few things you can improve.

  • Don't sleep the thread for half a second.
  • Drop the ping check (since the proxy might be behind a firewall and not responding to pings but still working)
  • Replace DownloadString with a HttpWebRequest getting the HEAD only.
  • Set the timeout of your HttpWebRequest to something lower than default (no need to wait that long. If a proxy doesn't respond within 10-20secs then you probably don't want to use it).
  • Split your big list into smaller ones and process them at the same time.

These alone should speed up your process by quite a bit.

As requested, here's an example of how to use 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);
}

I might do something like this:

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;
  }
}

Using a WhatIsMyIP-like REST service (I use one from https://TheProxIsRight.com).

Then As suggested above, I might try parallelize it with something like:

  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
    }
  });

Note, one can also use the REST API on the above site to query for working proxies: https://theproxisright.com/#apidemo

(Disclosure, I worked on the above site)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!