架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 22386|回复: 0

[资料] c# 验证过滤代理IP是否有效

[复制链接]
发表于 2016-5-20 11:22:50 | 显示全部楼层 |阅读模式
QQ截图20160520112055.jpg

  1. private void 导入IPToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             using (OpenFileDialog Openfile = new OpenFileDialog())
  4.             {
  5.                 Openfile.Filter = "文本文件|*.txt";
  6.                 Openfile.Multiselect = false;
  7.                 if (Openfile.ShowDialog() == DialogResult.OK)
  8.                 {
  9.                     Thread threadfile = new Thread(() => ReadFileIP(Openfile.FileName)) { IsBackground = true };
  10.                     threadfile.Start();
  11.                 }
  12.             }
  13.         }
  14.         /// <summary>
  15.         /// 读取txt代理ip
  16.         /// </summary>
  17.         /// <param name="filename"></param>
  18.         private void ReadFileIP(string filename)
  19.         {
  20.             txtmsg.BeginInvoke(new Action(() =>
  21.             {
  22.                 txtmsg.AppendText("开始导入IP代理!".SetLog());
  23.             }));
  24.             var file = File.Open(filename, FileMode.Open);
  25.             int num = 0;
  26.             int goods = 0;
  27.             int repeat = 0;
  28.             using (var stream = new StreamReader(file))
  29.             {
  30.                 while (!stream.EndOfStream)
  31.                 {
  32.                     num++;
  33.                     string linetemp = stream.ReadLine().Trim().ToLower();
  34.                     string[] iptxt = linetemp.Split(':');
  35.                     if (iptxt.Count() == 2)
  36.                     {
  37.                         lock (Config.lock_prxoy)
  38.                         {
  39.                             var data = Config._prxoyList.Where(m => m.ip == iptxt[0]).FirstOrDefault();
  40.                             if (data != null)
  41.                             {
  42.                                 repeat++;
  43.                                 continue;
  44.                             }
  45.                         }
  46.                         goods++;
  47.                         Model.ProxyIP _proxyip = new Model.ProxyIP();
  48.                         _proxyip.ip = iptxt[0];
  49.                         _proxyip.prot = int.Parse(iptxt[1]);
  50.                         ListViewItem item = new ListViewItem(_proxyip.ip);
  51.                         item.SubItems.Add(_proxyip.prot.ToString());
  52.                         item.SubItems.Add("");
  53.                         listViewIP.Invoke(new Action(() =>
  54.                         {
  55.                             ListViewItem itemresult = listViewIP.Items.Add(item);
  56.                             _dic.Add(_proxyip.ip, itemresult);
  57.                             //dic.Add(_send.Tel, backitem);
  58.                         }));
  59.                         lock (Config.lock_prxoy)
  60.                         {
  61.                             Config._prxoyList.Add(_proxyip);
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.             txtmsg.Invoke(new Action(() =>
  67.             {
  68.                 string log = string.Format("添加代理IP完成!有效数据为:{0},过滤重复数据:{1},总数据:{2}", goods.ToString(), repeat.ToString(), num.ToString());
  69.                 txtmsg.AppendText(log.SetLog());
  70.             }));
  71.             Thread filter = new Thread(new ThreadStart(filterIP)) { IsBackground = true };
  72.             filter.Start();
  73.         }
  74.         private void filterIP()
  75.         {
  76.             txtmsg.Invoke(new Action(() =>
  77.             {
  78.                 txtmsg.AppendText("正在过滤IP数据!".SetLog());
  79.             }));
  80.             List<System.Threading.Tasks.Task> TaskList = new List<System.Threading.Tasks.Task>();
  81.             lock (Config.lock_prxoy)
  82.             {
  83.                 foreach (Model.ProxyIP _model in Config._prxoyList)
  84.                 {
  85.                     var task = System.Threading.Tasks.Task.Factory.StartNew(() =>
  86.                     {
  87.                         bool reslut = VerIP(_model.ip, _model.prot);
  88.                         if (reslut)
  89.                         {
  90.                             _model.filter = Model.filterIP.有效;
  91.                             this.Invoke(new Action(() =>
  92.                             {
  93.                                 _dic[_model.ip].SubItems[2].Text = "有效";
  94.                             }));
  95.                         }
  96.                         else
  97.                         {
  98.                             _model.filter = Model.filterIP.无效;
  99.                             this.Invoke(new Action(() =>
  100.                             {
  101.                                 _dic[_model.ip].SubItems[2].Text = "无效";
  102.                             }));
  103.                         }
  104.                     });
  105.                     TaskList.Add(task);
  106.                 }
  107.             }
  108.             System.Threading.Tasks.Task.WaitAll(TaskList.ToArray());
  109.             txtmsg.Invoke(new Action(() =>
  110.             {
  111.                 txtmsg.AppendText(Config._prxoyList[0].filter.ToString() + "过滤IP数据完成!".SetLog());
  112.             }));
  113.         }

  114.         private bool VerIP(string ip,int port)
  115.         {
  116.             try
  117.             {
  118.                 HttpWebRequest Req;
  119.                 HttpWebResponse Resp;
  120.                 WebProxy proxyObject = new WebProxy(ip, port);// port为端口号 整数型
  121.                 Req = WebRequest.Create("https://www.baidu.com") as HttpWebRequest;
  122.                 Req.Proxy = proxyObject; //设置代理
  123.                 Req.Timeout = 1000;   //超时
  124.                 Resp = (HttpWebResponse)Req.GetResponse();
  125.                 Encoding bin = Encoding.GetEncoding("UTF-8");
  126.                 using (StreamReader sr = new StreamReader(Resp.GetResponseStream(), bin))
  127.                 {
  128.                     string str = sr.ReadToEnd();
  129.                     if (str.Contains("百度"))
  130.                     {
  131.                         Resp.Close();
  132.                         return true;
  133.                     }
  134.                     else
  135.                     {
  136.                         return false;
  137.                     }
  138.                 }
  139.                
  140.             }
  141.             catch (Exception ex)
  142.             {
  143.                 return false;
  144.             }
  145.         }
  146.     }
复制代码


主要的代码 ,我就贴上来了,那些model实体的,你们估计也用不到,这个过滤速度很快,哈哈。




上一篇:互联网行业让高盛态度有了新的想法
下一篇:asp.net关于参数里面带有#字符的问题
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-5-28 10:34

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表