Пример #1
0
        private List <WebProxy> GetProxiesByMaxResponseTime()
        {
            lock (_locker)
            {
                if (MemoryCache.Default.Get("GetProxies") != null)
                {
                    return((List <WebProxy>)MemoryCache.Default.Get("GetProxies"));
                }

                var maxResponseTime = double.Parse(ConfigurationManager.AppSettings["ProxyMaxResponseTime"]);

                var proxies = _proxyDataProvider.GetActiveProxies(maxResponseTime);

                _logger.InfoFormat("got [{0}] proxies from db < [{1}] sec response time", proxies.Count, maxResponseTime);

                var webProxies = new List <WebProxy>();

                foreach (var proxy in proxies)
                {
                    var proxyCredentials = new NetworkCredential(proxy["username"].ToString(), proxy["password"].ToString());
                    var webProxy         = new WebProxy(string.Format("{0}:{1}", proxy["ip"].ToString(), proxy["port"].ToString()));

                    webProxy.Credentials = proxyCredentials;
                    webProxies.Add(webProxy);
                }

                MemoryCache.Default.Add("GetProxies", webProxies, DateTimeOffset.Now.AddMinutes(1));

                return(webProxies);
            }
        }
Пример #2
0
        private List <WebProxy> GetProxies()
        {
            if (MemoryCache.Default.Get("GetProxies") != null)
            {
                return((List <WebProxy>)MemoryCache.Default.Get("GetProxies"));
            }

            var proxies = _proxyDataProvider.GetActiveProxies();

            _logger.InfoFormat("got [{0}] proxies from db < 0.5 sec response time", proxies.Count);

            var webProxies = new List <WebProxy>();

            foreach (var proxy in proxies)
            {
                var proxyCredentials = new NetworkCredential(proxy["username"].ToString(), proxy["password"].ToString());
                var webProxy         = new WebProxy(string.Format("{0}:{1}", proxy["ip"].ToString(), proxy["port"].ToString()));

                webProxy.Credentials = proxyCredentials;
                webProxies.Add(webProxy);
            }

            MemoryCache.Default.Add("GetProxies", webProxies, DateTimeOffset.Now.AddMinutes(1));

            return(webProxies);
        }