Есть ли вариант ускорить отсеивание нерабочих ******, просто , допустим если я 20 добавляю и из них только 2 рабочие проверка идет минут 7, как по мне долговато получается string Get(string proxy) { HttpRequest request = new HttpRequest(); request.KeepAlive = true; request.UserAgentRandomize(); request.Proxy = ProxyClient.Parse(ProxyType.HTTP, proxy); try { string response = request.Get("https://vk.com").ToString(); } catch (HttpException) { return null; } return "123"; } Код string Get(string proxy) { HttpRequest request = new HttpRequest(); request.KeepAlive = true; request.UserAgentRandomize(); request.Proxy = ProxyClient.Parse(ProxyType.HTTP, proxy); try { string response = request.Get("https://vk.com").ToString(); } catch (HttpException) { return null; } return "123"; }
DoplerVopler, чел , я говорю что вот эта функция долго работает , а ты мне расскажываешь как со списками работать
Класс Thread, асинхроннное, распараллеливание Пример с потоками: class Program { static void Main(string[] args) { Thread thread = new Thread(() => Get(/*тут будут *******/)); thread.Start(); Console.ReadKey(); } static string Get(string proxy) { try { WebProxy wp = new WebProxy(proxy, true); WebRequest wrq = WebRequest.Create("http://www.google.com"); wrq.Proxy = wp; WebResponse wrs = wrq.GetResponse(); Console.WriteLine("valid"); return proxy; } catch (Exception ex) { Console.WriteLine(ex.Message); return null; } } } Код class Program { static void Main(string[] args) { Thread thread = new Thread(() => Get(/*тут будут *******/)); thread.Start(); Console.ReadKey(); } static string Get(string proxy) { try { WebProxy wp = new WebProxy(proxy, true); WebRequest wrq = WebRequest.Create("http://www.google.com"); wrq.Proxy = wp; WebResponse wrs = wrq.GetResponse(); Console.WriteLine("valid"); return proxy; } catch (Exception ex) { Console.WriteLine(ex.Message); return null; } } }
N1ell, using System; using System.IO; using System.Threading.Tasks; using Leaf.xNet; private static void Main(string[] args) { const int threads = 1000; const string resultFile = "result.txt"; const string proxyFile = "proxies.txt"; var results = TextWriter.Synchronized(new StreamWriter(resultFile, false)); var proxies = File.ReadAllText(proxyFile).Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); Parallel.ForEach(proxies, new ParallelOptions { MaxDegreeOfParallelism = threads }, proxy => { HttpRequest req = null; try { req = new HttpRequest(); req.Proxy = Leaf.xNet.HttpProxyClient.Parse(proxy);/*только для http-проксей, можно поменять на sock4proxyclient или socks5proxyclient*/ string resp = req.Get("whoer.net").ToString(); if (req.Response.IsOK()) { results.WriteLine(proxy); } } catch (Exception ex) { string err = $"[ERROR]: {proxy}{Environment.NewLine}{ex.Message}"; Console.WriteLine(err); Console.WriteLine(); } finally { req?.Dispose(); } }); Console.WriteLine("Done!"); Console.ReadKey(); } Код using System; using System.IO; using System.Threading.Tasks; using Leaf.xNet; private static void Main(string[] args) { const int threads = 1000; const string resultFile = "result.txt"; const string proxyFile = "proxies.txt"; var results = TextWriter.Synchronized(new StreamWriter(resultFile, false)); var proxies = File.ReadAllText(proxyFile).Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); Parallel.ForEach(proxies, new ParallelOptions { MaxDegreeOfParallelism = threads }, proxy => { HttpRequest req = null; try { req = new HttpRequest(); req.Proxy = Leaf.xNet.HttpProxyClient.Parse(proxy);/*только для http-проксей, можно поменять на sock4proxyclient или socks5proxyclient*/ string resp = req.Get("whoer.net").ToString(); if (req.Response.IsOK()) { results.WriteLine(proxy); } } catch (Exception ex) { string err = $"[ERROR]: {proxy}{Environment.NewLine}{ex.Message}"; Console.WriteLine(err); Console.WriteLine(); } finally { req?.Dispose(); } }); Console.WriteLine("Done!"); Console.ReadKey(); } xNet качаешь отсюда >> https://github.com/csharp-leaf/Leaf.xNet В nuget он тоже есть если че.