コード例 #1
0
        public HttpStatusCode TestProxy(int id)
        {
            try
            {
                var proxy       = _proxyService.GetProxy(id);
                var testServers = _proxyTestServerService.GetTestProxies().ToList();

                var pingDate = ProxyHelper.Ping(proxy.Ip);

                if (pingDate.HasValue)
                {
                    proxy.LastFunctionalityTestDate = pingDate.Value;
                }

                var anonymity = ProxyHelper.CheckAnonymity(proxy.Ip, proxy.Port);

                if (anonymity != ProxyAnonymity.Unknown)
                {
                    proxy.Anonymity = anonymity;
                }

                var proxyTests = ProxyHelper.TestProxy(proxy, testServers);

                _proxyTestService.BatchCreateOrUpdate(proxyTests);

                _proxyService.Update(proxy);

                return(HttpStatusCode.OK);
            }
            catch
            {
                return(HttpStatusCode.InternalServerError);
            }
        }
コード例 #2
0
        public HttpStatusCode UpdateProxies()
        {
            var serviceProviders = _proxyProviderService.GetProxyProviders();
            var testServers      = _proxyTestServerService.GetTestProxies();

            Parallel.ForEach(serviceProviders, provider =>
            {
                try
                {
                    IList <Proxy> proxyList      = ProxyHelper.StartCrawler(provider);
                    provider.LastFetchOn         = DateTime.Now;
                    provider.LastFetchProxyCount = proxyList.Count;
                    provider.Exception           = "";

                    if (proxyList.Count > 0)
                    {
                        _proxyService.BatchCreateOrUpdate(proxyList);
                        IList <ProxyTest> proxyTestResults = ProxyHelper.TestProxies(proxyList.ToList(), testServers);

                        if (proxyTestResults.Count > 0)
                        {
                            _proxyTestService.BatchCreateOrUpdate(proxyTestResults);
                        }
                    }
                }
                catch (Exception ex)
                {
                    provider.LastFetchProxyCount = -1;
                    provider.Exception           = ex.InnerException?.Message;
                }

                _proxyProviderService.Update(provider);
            });

            try
            {
                _proxyProviderService.SaveChanges();
                return(HttpStatusCode.OK);
            }
            catch
            {
                return(HttpStatusCode.InternalServerError);
            }
        }