C # – comment tester si le proxy fonctionne ou non?

J’ai une assez grande liste de serveurs proxy et de leurs ports correspondants. Comment puis-je vérifier s’ils fonctionnent ou non?

Travail? Eh bien, vous devez les utiliser pour voir s’ils travaillent.

Si vous voulez voir s’ils sont en ligne, je suppose que le ping est une première étape.

Il existe une classe Ping dans .NET.

 using System.Net.NetworkInformation; private static bool CanPing(ssortingng address) { Ping ping = new Ping(); try { PingReply reply = ping.Send(address, 2000); if (reply == null) return false; return (reply.Status == IPStatus.Success); } catch (PingException e) { return false; } } 

J’aime faire une vérification WhatIsMyIP via un proxy comme test.

 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 { 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.ToSsortingng() + "|Connection Time:" + sw.Elapsed.TotalSeconds + "|ErrorMsg" + errorMsg); } }); } 

Cependant, je pourrais suggérer de tester explicitement différents types (par exemple, http, https, socks4, socks5). Ce qui précède ne vérifie que https. En construisant le ProxyChecker pour https://theproxisright.com/#proxyChecker , j’ai commencé avec le code ci-dessus, puis j’ai dû développer pour d’autres capacités / types.

essaye ça:

 public static bool SoketConnect(ssortingng host, int 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(host); var ipep = new IPEndPoint(hip, port); connsock.Connect(ipep); if (connsock.Connected) { is_success = true; } connsock.Close(); } catch (Exception) { is_success = false; } return is_success; } 
 ssortingng strIP = "10.0.0.0"; int intPort = 12345; public static bool PingHost(ssortingng strIP , int intPort ) { bool blProxy= false; try { TcpClient client = new TcpClient(strIP ,intPort ); blProxy = true; } catch (Exception ex) { MessageBox.Show("Error pinging host:'" + strIP + ":" + intPort .ToSsortingng() + "'"); return false; } return blProxy; } public void Proxy() { bool tt = PingHost(strIP ,intPort ); if(tt == true) { MessageBox.Show("tt True"); } else { MessageBox.Show("tt False"); }