コード例 #1
0
 public void OnProxyResult(ProxyCheckResult e)
 {
     lock (proxyDataGridLock)
     {
         if ((e.Proxy != null) && (e.Proxy.Address == null))
         {
             //No address value in the proxy means the request did not use a proxy
             //thus it is a result from a connection test without a proxy.
             string resultText = null;
             if (e.Result)
             {
                 resultText = TEST_SUCCESSFUL;
             }
             else
             {
                 resultText = TEST_FAILED;
             }
             MessageBox.Show(resultText, "Test results");
         }
         else
         {
             Action <string, ListBox, Label> start = new Action <string, ListBox, Label>(this.AddProxyToList);
             if (e.Result)
             {
                 this.Invoke(start, e.AddressAndPort, WorkingProxyList, WorkingProxyListCountText);
             }
             else
             {
                 this.Invoke(start, e.AddressAndPort, FailedProxyList, FailedProxyListCountText);
             }
         }
     }
 }
コード例 #2
0
        public static async Task <ProxyCheckResult> CheckProxyAsync(
            WebProxy proxy,
            string website,
            int timeoutSecs)
        {
            using (HttpClientHandler clienthandler = new HttpClientHandler()
            {
                Proxy = proxy, UseProxy = true
            }) {
                using (HttpClient httpClient = new HttpClient(clienthandler)
                {
                    Timeout = new TimeSpan(0, 0, timeoutSecs)
                }) {
                    ProxyCheckResult result = ProxyCheckResult.UNKNOWN;

                    try {
                        HttpResponseMessage resp = await httpClient.GetAsync(website);

                        switch (resp.StatusCode)
                        {
                        case HttpStatusCode.OK:
                            result = ProxyCheckResult.OK;
                            break;

                        default:
                            result = ProxyCheckResult.UNKNOWN;
                            break;
                        }
                    }
                    catch (Exception ex) {
                        if (ex is TaskCanceledException || ex is HttpRequestException)
                        {
                            result = ProxyCheckResult.TIMED_OUT;
                        }
                    }

                    return(result);
                }
            }
        }
コード例 #3
0
        public static async Task CheckProxiesAsync(
            List <WebProxy> proxies,
            string website,
            int timeoutSecs,
            IProgress <ProxyCheckProgressReport> progress,
            CancellationToken cancellationToken)
        {
            int numTotal  = proxies.Count;
            int chunkSize = Math.Min(ChunkSize, proxies.Count);

            foreach (List <WebProxy> splitProxies in ListExtensions.ChunkBy(proxies, chunkSize))
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }

                List <Task> tasks = new List <Task>();

                foreach (WebProxy proxy in splitProxies)
                {
                    tasks.Add(Task.Run(async() => {
                        ProxyCheckResult result = await CheckProxyAsync(proxy, website, timeoutSecs);

                        progress.Report(new ProxyCheckProgressReport()
                        {
                            NumTotal         = numTotal,
                            ProxyChecked     = proxy,
                            ProxyCheckResult = result
                        });
                    }));
                }

                await Task.WhenAll(tasks);
            }
        }