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实体的,你们估计也用不到,这个过滤速度很快,哈哈。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值