【问题标题】:C# Test if proxies are working before using themC# 在使用代理之前测试它们是否正常工作
【发布时间】:2016-04-07 04:14:10
【问题描述】:

我想在使用之前检查我列表中的代理是否正常工作,可以吗?

HTTP/HTTPS 很容易,因为你只需要使用 webclient 而不是 socks?

我认为这对所有人都有效

public static bool SoketConnect(string addresse)
    {
        string[] proxy = addresse.Split(':');
        if (proxy.Count() == 2)
        {
            string host = proxy[0];
            int port = Convert.ToInt32(proxy[1]);

            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(host);
                var ipep = new IPEndPoint(hip, port);
                connsock.Connect(ipep);
                    try
                    {
                        byte[] outStream = System.Text.Encoding.ASCII.GetBytes("ping");
                        byte[] inStream = new byte[100];
                        connsock.Send(outStream);
                        connsock.ReceiveTimeout = 500;
                        connsock.Receive(inStream);
                        string message = System.Text.Encoding.ASCII.GetString(inStream);
                        foreach (var bytes in inStream)
                        {
                            if (bytes != 0)
                            {
                                is_success = true;
                                break;
                            }
                        }
                        connsock.Disconnect(false);
                    }
                    catch(Exception ex)
                    {
                        is_success = false;
                    }
                connsock.Close();
            }
            catch (Exception ex)
            {
                is_success = false;
            }
            return is_success;
        }
        else
            return false;
    }

但我总是遇到异常

"底层连接已关闭:连接已关闭 意外。” System.Net.WebException

tldr;如何检查我的 socks 代理是否正常工作(回到如何使用 socks 代理)

编辑:仍然没有帮助,需要答案 EDIT2:只 ping 代理不是正确的做法

【问题讨论】:

    标签: c# http testing proxy socks


    【解决方案1】:

    据我所知,您必须使用它们来查看它们是否正常工作。我建议以下之一:

    1. ping - Ping 类在:

      using System.Net.NetworkInformation;
      
      private static bool CanPing(string address)
      {
          Ping myping = new Ping();
      
          try
          {
              PingReply reply = myping.Send(address, 2000);
      
              if (reply == null) 
                  return false;
      
              return (reply.Status == IPStatus.Success);
          }
          catch (PingException e)
          {
              return false;
          }
      }
      
    2. 执行“我的 IP 是什么?”通过代理:

      public static void TestProxies() 
      {
          var lowp = new List<WebProxy> { 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 
                    {
                      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();
              }
            });
      }
      

    【讨论】:

    • 对于 ping,ping 代理不会告诉它是否不起作用,第二部分,它是否适用于 HTTP 和 SOCKS?
    猜你喜欢
    • 2011-02-21
    • 2014-06-18
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 2016-08-03
    相关资源
    最近更新 更多