Example #1
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();
 }
Example #2
0
        /// <summary>
        /// 通过在线代理直接获取未验证的IP代理
        /// 主要用于本地IP代理库存不足,临时获取
        /// </summary>
        /// <returns></returns>
        public ProxyEntity GetHttProxyEntity()
        {
            var ipList    = GetProxyByHttp();
            var ipAndPort = ipList.Split(':');
            var model     = new ProxyEntity();

            model.IpAddress   = ipAndPort[0];
            model.Port        = Conv.ToInt(ipAndPort[1]);
            model.Usage       = 1;
            model.CanUse      = true;
            model.CreateTime  = DateTime.Now;
            model.LastUseTime = DateTime.Now;
            ProxyDomain proxyDomain = new ProxyDomain();

            if (!proxyDomain.IsExist(model))
            {
                proxyDomain.Add(model);
            }
            return(model);
        }