How to test if a proxy server is working or not?

前端 未结 4 1922
长情又很酷
长情又很酷 2020-12-31 09:38

I\'ve got a pretty big list with proxy servers and their corresponding ports. How can I check, if they are working or not?

4条回答
  •  醉话见心
    2020-12-31 10:13

    I like to do a WhatIsMyIP check through a proxy as a test.

    using RestSharp;
    
    public static void TestProxies() {
      var lowp = new List { new WebProxy("1.2.3.4", 8080), new WebProxy("5.6.7.8", 80) };
    
      Parallel.ForEach(lowp, wp => {
        var success = false;
        var errorMsg = "";
        var sw = new Stopwatch();
        try {
          sw.Start();
          var response = new RestClient {
            //this site is no longer up
            BaseUrl = "https://webapi.theproxisright.com/",
            Proxy = wp
          }.Execute(new RestRequest {
            Resource = "api/ip",
            Method = Method.GET,
            Timeout = 10000,
            RequestFormat = DataFormat.Json
          });
          if (response.ErrorException != null) {
            throw response.ErrorException;
          }
          success = (response.Content == wp.Address.Host);
        } catch (Exception ex) {
          errorMsg = ex.Message;
        } finally {
          sw.Stop();
          Console.WriteLine("Success:" + success.ToString() + "|Connection Time:" + sw.Elapsed.TotalSeconds + "|ErrorMsg" + errorMsg);
        }
      });
    }
    

    However, I might suggest testing explicitly for different types (ie http, https, socks4, socks5). The above only checks https. In building the ProxyChecker for https://theproxisright.com/#proxyChecker, I started w/ the code above, then eventually had to expand for other capabilities/types.

提交回复
热议问题