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