public static bool CheckResponse(ProxyCardCheckCache proxy) { var result = false; HttpWebResponse response = null; DCT.Execute(d => { WebRequest.DefaultWebProxy = new WebProxy { Address = new Uri("http://" + proxy.Address + ":" + proxy.Port) }; var request = WebRequest.Create("https://avito.ru"); request.Timeout = 5000; try { response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { result = true; } } catch (Exception ex) { } finally { if (response != null) { response.Close(); } } }); return(result); }
static void TestProxy() { var avito = new Avito(); var login = "******"; var password = "******"; var url = "https://www.avito.ru"; var ipExceptionsString = new[] { "Доступ с вашего IP-адреса временно ограничен", "Доступ временно заблокирован", "Ошибка при установлении защищённого соединения" }; var successCount = 0; for (var i = 0; i < 100; i++) { ProxyCardCheckCache proxy = ProxyHelper.GetProxy(url, ipExceptionsString, 1000); if (proxy == null) { ConsoleHelper.SendException($"Command execute crash and stoped, proxy not found or service not available"); //throw new Exception("Command execute crash and stoped, proxy not found or service not available"); } else { FirefoxHelper.ExecuteWithVisual(browser => { browser.Navigate().GoToUrl("https://www.avito.ru/moskva/bytovaya_elektronika"); var success = avito.Auth(browser, login, password); if (success) { successCount++; } }, proxy, 50, true); } } ConsoleHelper.SendException($"Success count is {successCount}"); }
public static bool CheckProxy() { var result = false; DCT.Execute(d => { var foundedProxy = false; var count = 0; while (!foundedProxy) { var nextProxy = CollectorModels.Service.ProxyClientHelper.Next(); if (nextProxy == null) { Console.WriteLine("Proxy is null"); continue; } Console.WriteLine($"Proxy {count}|Avito:{nextProxy.Avito}|Https:{nextProxy.Https}|Http:{nextProxy.Http}|Google:{nextProxy.Google}|Ping:{nextProxy.PingLast}|={nextProxy.Address}:{nextProxy.Port}"); if (!CheckResponse(nextProxy)) { continue; } currentProxy = nextProxy; foundedProxy = true; result = true; count++; } }); return(result); }
public static void ExecuteWithVisual(Action <FirefoxDriver> executeAction, ProxyCardCheckCache proxyCache = null, int Timeout = 20, bool withImages = false) { DCT.Execute(c => { FirefoxOptions options = new FirefoxOptions(); if (proxyCache != null) { var proxy = new Proxy(); proxy.HttpProxy = $"{proxyCache.Address}:{proxyCache.Port}"; proxy.FtpProxy = $"{proxyCache.Address}:{proxyCache.Port}"; proxy.SslProxy = $"{proxyCache.Address}:{proxyCache.Port}"; options.Proxy = proxy; } //options.AddArguments("--headless"); options.Profile = new FirefoxProfile(); options.Profile.SetPreference("dom.disable_beforeunload", true); options.Profile.SetPreference("dom.popup_maximum", 0); options.Profile.SetPreference("privacy.popups.showBrowserMessage", false); options.Profile.SetPreference("pdfjs.disabled", true); if (!withImages) { options.Profile.SetPreference("permissions.default.image", 2); options.Profile.SetPreference("dom.ipc.plugins.enabled.libflashplayer.so", "false"); } FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(); service.SuppressInitialDiagnosticInformation = false; service.HideCommandPromptWindow = true; using (var currentDriver = new FirefoxDriver(service, options, TimeSpan.FromSeconds(Timeout))) { try { var manager = currentDriver.Manage(); manager.Window.Minimize(); manager.Timeouts().ImplicitWait = TimeSpan.FromSeconds(Timeout); manager.Timeouts().PageLoad = TimeSpan.FromSeconds(Timeout); executeAction?.Invoke(currentDriver); } catch (Exception ex) { ConsoleHelper.SendMessage($"FIREFOXDRIVER CRASHED {Environment.NewLine} {ex.ToString()}"); } try { currentDriver.Close(); currentDriver.Quit(); } catch (Exception ex) { ConsoleHelper.SendMessage($"FIREFOXDRIVER CLOSE AND QUIT CRASHED {Environment.NewLine} {ex.ToString()}"); } } }, continueExceptionMethod: (ex, c) => { }); }
public static void CloseDriver() { currentProxy = null; if (driver != null) { driver.Close(); driver.Quit(); driver.Dispose(); } }
private static string proxyCheck(string URL, ProxyCardCheckCache proxyCache, int timeout = 5000) { var result = ""; HttpStatusCode status = HttpStatusCode.RequestTimeout; try { var text = ""; var proxy = new WebProxy(proxyCache.Address, Convert.ToInt32(proxyCache.Port)); var sw = new Stopwatch(); sw.Start(); using (var client = new HttpClient(new HttpClientHandler() { Proxy = proxy })) { client.Timeout = TimeSpan.FromMilliseconds(timeout); var request = new HttpRequestMessage(HttpMethod.Get, URL); var response = client.SendAsync(request).Result; status = response.StatusCode; text = response.Content.ReadAsStringAsync().Result; } sw.Stop(); if (status != null && status == HttpStatusCode.OK) { var p = Convert.ToInt32(sw.ElapsedMilliseconds); if (p < timeout) { result = text; } } } catch (Exception ex) { ConsoleHelper.SendMessage($"Proxy don't worked - {proxyCache.Address}:{proxyCache.Port}. Connect status {status}"); } return(result); }
public static ProxyCardCheckCache GetProxy(string URL, IEnumerable <string> exceptionString, int timeout = 5000) { ProxyCardCheckCache result = null; DCT.Execute(c => { ConsoleHelper.SendMessage($"GetProxy started"); while (true) { var proxy = CollectorModels.Service.ProxyClientHelper.Next(); if (proxy == null) { ConsoleHelper.SendMessage($"Proxy service crash or not available now"); return; } var text = proxyCheck(URL, proxy, timeout); if (!string.IsNullOrWhiteSpace(text)) { if (exceptionString != null && exceptionString.Any()) { foreach (var str in exceptionString) { if (text.Contains(str)) { continue; } } } result = proxy; ConsoleHelper.SendMessage($"Get proxy completed"); return; } } }); return(result); }