Example #1
0
        /// <summary>
        /// 验证Ip可用性
        /// </summary>
        /// <param name="maxThreadNum">启用多少线程验证</param>
        public void ValidateCanUse(int maxThreadNum = 1)
        {
            var tasks = new Task[maxThreadNum];

            for (var i = 0; i < maxThreadNum; i++)
            {
                tasks[i] = new Task(() =>
                {
                    for (var j = 0; j < MaxValue; j++)
                    {
                        ProxyDomain proxyDomain = new ProxyDomain();
                        var id          = new Random().Next(1, proxyDomain.GetMaxId());
                        var proxyEntity = proxyDomain.GetById(id);
                        if (proxyEntity.Id == 0)
                        {
                            continue;
                        }
                        if (ThreadValidate.Doit(proxyEntity))
                        {
                            break;
                        }
                    }
                    Console.WriteLine("完成");
                });
                tasks[i].Start();
            }
            Task.WaitAll(tasks);
        }
Example #2
0
 /// <summary>
 /// 直接采集IP并更新到数据库
 /// </summary>
 /// <param name="countNum">采集IP总数</param>
 /// <param name="getNum">每次提取数量</param>
 /// <param name="isValidate">是否对代理验证</param>
 public void ProxySave(int countNum = 100, int getNum = 5, bool isValidate = false)
 {
     new Thread(() =>
     {
         while (true)
         {
             if (countNum == 0)
             {
                 break;
             }
             var ipLists = GetProxyByHttp(getNum).Split(Environment.NewLine.ToCharArray()).ExceptNull().ToList();
             foreach (var ipList in ipLists)
             {
                 var ipAndPort = ipList.Split(':');
                 if (string.IsNullOrWhiteSpace(ipList) || ipAndPort.Length < 2)
                 {
                     continue;
                 }
                 Console.WriteLine("获取到代理:" + ipList);
                 var model               = new ProxyEntity();
                 model.IpAddress         = ipAndPort[0];
                 model.Port              = Conv.ToInt(ipAndPort[1]);
                 model.Usage             = 0;
                 model.CreateTime        = DateTime.Now;
                 ProxyDomain proxyDomain = new ProxyDomain();
                 if (isValidate)
                 {
                     if (ThreadValidate.VerificationIp(model.IpAddress, model.Port))
                     {
                         if (!proxyDomain.IsExist(model))
                         {
                             proxyDomain.Add(model);
                         }
                         countNum--;
                     }
                 }
                 if (!proxyDomain.IsExist(model))
                 {
                     proxyDomain.Add(model);
                 }
                 countNum--;
             }
         }
         Console.WriteLine("IP采集完毕");
     })
     {
         IsBackground = false
     }.Start();
 }