zoukankan      html  css  js  c++  java
  • 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实体的,你们估计也用不到,这个过滤速度很快,哈哈。

  • 相关阅读:
    java并发计算的几种基本使用示例
    axios、ajax和xhr前端发送测试
    Spring注解
    Android菜鸟教程笔记
    普通二叉树操作
    MyBatis
    mysql的select语句总结与索引使用
    sys.argv的意义[转]
    硬件小白学习之路(1)稳压芯片LM431
    FPGA小白学习之路(6)串口波特率问题的处理
  • 原文地址:https://www.cnblogs.com/testsec/p/6095842.html
Copyright © 2011-2022 走看看