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 = ;
int goods = ;
int repeat = ;
using (var stream = new StreamReader(file))
{
while (!stream.EndOfStream)
{
num++;
string linetemp = stream.ReadLine().Trim().ToLower();
string[] iptxt = linetemp.Split(':');
if (iptxt.Count() == )
{
lock (Config.lock_prxoy)
{
var data = Config._prxoyList.Where(m => m.ip == iptxt[]).FirstOrDefault();
if (data != null)
{
repeat++;
continue;
}
}
goods++;
Model.ProxyIP _proxyip = new Model.ProxyIP();
_proxyip.ip = iptxt[];
_proxyip.prot = int.Parse(iptxt[]);
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[].Text = "有效";
}));
}
else
{
_model.filter = Model.filterIP.无效;
this.Invoke(new Action(() =>
{
_dic[_model.ip].SubItems[].Text = "无效";
}));
}
});
TaskList.Add(task);
}
}
System.Threading.Tasks.Task.WaitAll(TaskList.ToArray());
txtmsg.Invoke(new Action(() =>
{
txtmsg.AppendText(Config._prxoyList[].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 = ; //超时
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实体的,你们估计也用不到,这个过滤速度很快,哈哈。

C# 验证过滤代理IP是否有效的更多相关文章

  1. python爬虫成长之路(二):抓取代理IP并多线程验证

    上回说到,突破反爬虫限制的方法之一就是多用几个代理IP,但前提是我们得拥有有效的代理IP,下面我们来介绍抓取代理IP并多线程快速验证其有效性的过程. 一.抓取代理IP 提供免费代理IP的网站还挺多的, ...

  2. 维护爬虫代理IP池--采集并验证

    任务分析 我们爬的免费代理来自于https://www.kuaidaili.com这个网站.用`requests`将ip地址与端口采集过来,将`IP`与`PORT`组合成`requests`需要的代理 ...

  3. Python3.x:免费代理ip的批量获取并入库

    Python3.x:免费代理ip的批量获取并入库 一.简介 网络爬虫的世界,向来都是一场精彩的攻防战.现在许多网站的反爬虫机制在不断的完善,其中最令人头疼的,莫过于直接封锁你的ip.但是道高一尺魔高一 ...

  4. 【原创】验证代理IP是否有用

    /// <summary> /// 验证代理IP是否有用 /// </summary> /// <param name="ip">IP地址< ...

  5. 爬虫爬取代理IP池及代理IP的验证

    最近项目内容需要引入代理IP去爬取内容. 为了项目持续运行,需要不断构造.维护.验证代理IP. 为了绕过服务端对IP 和 频率的限制,为了阻止服务端获取真正的主机IP. 一.服务器如何获取客户端IP ...

  6. 代理IP爬取和验证(快代理&西刺代理)

    前言 仅仅伪装网页agent是不够的,你还需要一点新东西 今天主要讲解两个比较知名的国内免费IP代理网站:西刺代理&快代理,我们主要的目标是爬取其免费的高匿代理,这些IP有两大特点:免费,不稳 ...

  7. Python 快速验证代理IP是否有效

    有时候,我们需要用到代理IP,比如在爬虫的时候,但是得到了IP之后,可能不知道怎么验证这些IP是不是有效的,这时候我们可以使用Python携带该IP来模拟访问某一个网站,如果多次未成功访问,则说明这个 ...

  8. 验证代理IP

    ##author:wuhao#import urllib.requestfrom http import cookiejar import xlrd import threading #有效的代理,可 ...

  9. Java 验证代理ip

    原文地址:http://www.cnblogs.com/junrong624/p/5416503.html 1 import java.io.IOException; import java.io.I ...

随机推荐

  1. tf.square

    tf.square square( x, name=None ) 功能说明: 计算张量对应元素平方 参数列表: 参数名 必选 类型 说明 x 是 张量 是 half, float32, float64 ...

  2. mybatis中mapUnderscoreToCamelCase自动驼峰命名转换

    ssm项目中在mybatis配置文件中添加以下配置,可以将数据库中user_name转化成userName与实体类属性对应,如果数据库使用如user_name的命名方式,实体类采用驼峰命名.配置后无需 ...

  3. node.js官方文档chm电子书的制作

    制作软件:WebCHMSetup2.22.zip,http://www.onlinedown.net/soft/31553.htm 制作好的电子书:Node.js(v6.10.2).zip 参考链接: ...

  4. 3. 集成学习(Ensemble Learning)随机森林(Random Forest)

    1. 集成学习(Ensemble Learning)原理 2. 集成学习(Ensemble Learning)Bagging 3. 集成学习(Ensemble Learning)随机森林(Random ...

  5. Linux 下编译出现 undefined reference to `pthread_create'

    这是由于没有链接线程库的原因,只要在编译的时候加入: -lpthread 参数即可. arm-linux-gcc serial.c -o serial -lpthread 查看 ubuntu 版本的命 ...

  6. [uboot]uboot中am437对应的GPIO配置

    修改文件:board/ti/am43xx/mux.c 修改内容:void enable_board_pin_mux(configure_module_pin_mux(d3d4_pin_mux)

  7. JMX超详细解读<转>

    一.JMX的定义 JMX(Java Management Extensions)是一个为应用程序植入管理功能的框架.JMX是一套标准的代理和服务,实际上,用户可以在任何Java应用程序中使用这些代理和 ...

  8. STM32cube库配置双ADC的同步规则采样

    http://www.stmcu.org/module/forum/forum.php?mod=viewthread&tid=605203&extra=page%3D&page ...

  9. mac OS X:[11]如何添加打印机

    苹果菜单中,单击下拉列表中的『系统偏好设置』: 或在Dock上,单击『系统偏好设置』图标. 2 在『系统偏好设置』窗口中,单击『打印机与扫描仪』图标. 3 在『打印机与扫描仪』窗口中,单击打印机框架下 ...

  10. windows 7 下安装 vagrant + Oracle VM VirtualBox

    一.安装下准备 1.下载Oracle VM VirtualBox https://www.virtualbox.org/wiki/Downloads  (VirtualBox-4.3.22-98236 ...