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

  • 相关阅读:
    Python深入03 对象的属性
    利用Webkit抓取动态网页和链接
    分享:OCILIB 3.11.0 发布,跨平台 Oracle 驱动
    Knockoutjs实战开发:控制子绑定(control descendant bindings)
    利用InjectedBundle定制自己的Webkit(二)
    使用solrj和EasyNet.Solr进行原子更新
    Chaos网络库(二) Buffer的设计
    分享:djangohaystack+solr实现搜索
    Moon.ORM 4.4 隆重发布,在性能和使用便捷上一挑群雄(mysoft,cyq,pdf)
    数据结构利器之私房STL(中)
  • 原文地址:https://www.cnblogs.com/testsec/p/6095842.html
Copyright © 2011-2022 走看看