C# 驗證過濾代理IP是否有效


private void 導入IPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog Openfile = new OpenFileDialog())
            {
                Openfile.Filter = "文本文件|*.txt";
                Openfile.Multiselect = false;
                if (Openfile.ShowDialog() == DialogResult.OK)
                {
                    Thread threadfile = new Thread(() => ReadFileIP(Openfile.FileName)) { IsBackground = true };
                    threadfile.Start();
                }
            }
        }
        /// <summary>
        /// 讀取txt代理ip
        /// </summary>
        /// <param name="filename"></param>
        private void ReadFileIP(string filename)
        {
            txtmsg.BeginInvoke(new Action(() =>
            {
                txtmsg.AppendText("開始導入IP代理!".SetLog());
            }));
            var file = File.Open(filename, FileMode.Open);
            int num = 0;
            int goods = 0;
            int repeat = 0;
            using (var stream = new StreamReader(file))
            {
                while (!stream.EndOfStream)
                {
                    num++;
                    string linetemp = stream.ReadLine().Trim().ToLower();
                    string[] iptxt = linetemp.Split(':');
                    if (iptxt.Count() == 2)
                    {
                        lock (Config.lock_prxoy)
                        {
                            var data = Config._prxoyList.Where(m => m.ip == iptxt[0]).FirstOrDefault();
                            if (data != null)
                            {
                                repeat++;
                                continue;
                            }
                        }
                        goods++;
                        Model.ProxyIP _proxyip = new Model.ProxyIP();
                        _proxyip.ip = iptxt[0];
                        _proxyip.prot = int.Parse(iptxt[1]);
                        ListViewItem item = new ListViewItem(_proxyip.ip);
                        item.SubItems.Add(_proxyip.prot.ToString());
                        item.SubItems.Add("");
                        listViewIP.Invoke(new Action(() =>
                        {
                            ListViewItem itemresult = listViewIP.Items.Add(item);
                            _dic.Add(_proxyip.ip, itemresult);
                            //dic.Add(_send.Tel, backitem);
                        }));
                        lock (Config.lock_prxoy)
                        {
                            Config._prxoyList.Add(_proxyip);
                        }
                    }
                }
            }
            txtmsg.Invoke(new Action(() =>
            {
                string log = string.Format("添加代理IP完成!有效數據為:{0},過濾重復數據:{1},總數據:{2}", goods.ToString(), repeat.ToString(), num.ToString());
                txtmsg.AppendText(log.SetLog());
            }));
            Thread filter = new Thread(new ThreadStart(filterIP)) { IsBackground = true };
            filter.Start();
        }
        private void filterIP()
        {
            txtmsg.Invoke(new Action(() =>
            {
                txtmsg.AppendText("正在過濾IP數據!".SetLog());
            }));
            List<System.Threading.Tasks.Task> TaskList = new List<System.Threading.Tasks.Task>();
            lock (Config.lock_prxoy)
            {
                foreach (Model.ProxyIP _model in Config._prxoyList)
                {
                    var task = System.Threading.Tasks.Task.Factory.StartNew(() =>
                    {
                        bool reslut = VerIP(_model.ip, _model.prot);
                        if (reslut)
                        {
                            _model.filter = Model.filterIP.有效;
                            this.Invoke(new Action(() =>
                            {
                                _dic[_model.ip].SubItems[2].Text = "有效";
                            }));
                        }
                        else
                        {
                            _model.filter = Model.filterIP.無效;
                            this.Invoke(new Action(() =>
                            {
                                _dic[_model.ip].SubItems[2].Text = "無效";
                            }));
                        }
                    });
                    TaskList.Add(task);
                }
            }
            System.Threading.Tasks.Task.WaitAll(TaskList.ToArray());
            txtmsg.Invoke(new Action(() =>
            {
                txtmsg.AppendText(Config._prxoyList[0].filter.ToString() + "過濾IP數據完成!".SetLog());
            }));
        }

        private bool VerIP(string ip,int port)
        {
            try
            {
                HttpWebRequest Req;
                HttpWebResponse Resp;
                WebProxy proxyObject = new WebProxy(ip, port);// port為端口號 整數型
                Req = WebRequest.Create("https://www.baidu.com") as HttpWebRequest;
                Req.Proxy = proxyObject; //設置代理
                Req.Timeout = 1000;   //超時
                Resp = (HttpWebResponse)Req.GetResponse();
                Encoding bin = Encoding.GetEncoding("UTF-8");
                using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), bin))
                {
                    string str = sr.ReadToEnd();
                    if (str.Contains("百度"))
                    {
                        Resp.Close();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }

主要的代碼 ,我就貼上來了,那些model實體的,你們估計也用不到,這個過濾速度很快,哈哈。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM