Пример #1
0
        /// <summary>
        /// 验Ip线程函数
        /// </summary>
        /// <param name="index">代理ip地址在APIList中的索引</param>
        /// <param name="checknum">验ip线程数</param>
        public static void IPCheckFunc(String api = "", int index = 1, int checknum = 100, int limit = 1000, int delay = 5000)
        {
            String API_Url = String.IsNullOrEmpty(api)?String.Format(API_Url_List[index], limit):api;
            var    ips     = HttpMethod.InputApi(API_Url);

            Thread[] tarr = new Thread[checknum];
            Task.Factory.StartNew(() =>
            {
                while (IPCheckStack.Count < 500)
                {
                    if (ips.Count == 0)
                    {
                        ips = HttpMethod.InputApi(API_Url);
                    }
                    Thread.Sleep(delay);
                }
            });


            for (int i = 0; i < checknum; i++)
            {
                tarr[i] = new Thread(new ThreadStart(delegate {
                    String ip             = String.Empty;
                    HttpHelper helper     = new HttpHelper();
                    HttpItem item         = new HttpItem();
                    HttpResult result     = new HttpResult();
                    item.URL              = "http://2017.ip138.com/ic.asp";
                    item.Timeout          = 6000;
                    item.ReadWriteTimeout = 8000;
                    item.UserAgent        = "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.04";
                    String Ips            = String.Empty;
                    while (true)
                    {
                        if (IPCheckStack.Count < 500)
                        {
                            if (ips.TryPop(out ip))
                            {
                                item.ProxyIp = ip;
                                try
                                {
                                    if (helper.GetHtml(item).Html.Contains(ip.Substring(0, ip.IndexOf(":"))))
                                    {
                                        IPCheckStack.Push(ip);
                                    }
                                }
                                catch
                                {
                                }
                            }
                            else
                            {
                                Thread.Sleep(1000);
                            }
                        }
                        else
                        {
                            Thread.Sleep(5000);
                        }
                    }
                }));
                tarr[i].IsBackground = true;
                tarr[i].Start();
            }
        }