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 = ;
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是否有效的更多相关文章
- python爬虫成长之路(二):抓取代理IP并多线程验证
上回说到,突破反爬虫限制的方法之一就是多用几个代理IP,但前提是我们得拥有有效的代理IP,下面我们来介绍抓取代理IP并多线程快速验证其有效性的过程. 一.抓取代理IP 提供免费代理IP的网站还挺多的, ...
- 维护爬虫代理IP池--采集并验证
任务分析 我们爬的免费代理来自于https://www.kuaidaili.com这个网站.用`requests`将ip地址与端口采集过来,将`IP`与`PORT`组合成`requests`需要的代理 ...
- Python3.x:免费代理ip的批量获取并入库
Python3.x:免费代理ip的批量获取并入库 一.简介 网络爬虫的世界,向来都是一场精彩的攻防战.现在许多网站的反爬虫机制在不断的完善,其中最令人头疼的,莫过于直接封锁你的ip.但是道高一尺魔高一 ...
- 【原创】验证代理IP是否有用
/// <summary> /// 验证代理IP是否有用 /// </summary> /// <param name="ip">IP地址< ...
- 爬虫爬取代理IP池及代理IP的验证
最近项目内容需要引入代理IP去爬取内容. 为了项目持续运行,需要不断构造.维护.验证代理IP. 为了绕过服务端对IP 和 频率的限制,为了阻止服务端获取真正的主机IP. 一.服务器如何获取客户端IP ...
- 代理IP爬取和验证(快代理&西刺代理)
前言 仅仅伪装网页agent是不够的,你还需要一点新东西 今天主要讲解两个比较知名的国内免费IP代理网站:西刺代理&快代理,我们主要的目标是爬取其免费的高匿代理,这些IP有两大特点:免费,不稳 ...
- Python 快速验证代理IP是否有效
有时候,我们需要用到代理IP,比如在爬虫的时候,但是得到了IP之后,可能不知道怎么验证这些IP是不是有效的,这时候我们可以使用Python携带该IP来模拟访问某一个网站,如果多次未成功访问,则说明这个 ...
- 验证代理IP
##author:wuhao#import urllib.requestfrom http import cookiejar import xlrd import threading #有效的代理,可 ...
- Java 验证代理ip
原文地址:http://www.cnblogs.com/junrong624/p/5416503.html 1 import java.io.IOException; import java.io.I ...
随机推荐
- js 时间格式化和时间戳
formatMessageTime: function (time) { var week = { "0": "星期日", "1": &qu ...
- Arrays.sort()的底层实现
1.基本类型(以int为例) 源码中的快速排序,主要做了以下几个方面的优化: 1)当待排序的数组中的元素个数较少时,源码中的阀值为7,采用的是插入排序.尽管插入排序的时间复杂度为0(n^2),但是当数 ...
- c++之五谷杂粮---1
1. 位运算符,如果运算对象是带符号的且它的值为负,那么位运算符如何处理运算对象的“符号位”依赖于机器.此时左移操作可能会改变符号位的值,因此是一种UB. Best Practices: 关于符号位 ...
- Eigen教程(5)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 块操作 块是matrix或array中的矩形子部分. 使用块 函数.block(), ...
- 基于animation.css实现动画旋转特效
分享一款基于animation.css实现动画旋转特效.这是一款基于CSS3实现的酷炫的动画旋转特效代码.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div class ...
- MYSQL自动备份策略的选择与实践
目前流行几种备份方式: 1.逻辑备份:使用mysql自带的mysqldump工具进行备份.备份成sql文件形式.优点:最大好处是能够与正在运行的mysql自动协同工作,在运行期间可以确保备份是当时的点 ...
- 【Unity】自定义编辑器窗口——拓展编辑器功能
最近学习了Unity自定义编辑器窗口,下面简单总结,方便用到时回顾. 新建一个脚本: using UnityEngine; using System.Collections; using UnityE ...
- mybatis注解方式批量插入数据
@Insert("<script>" + "INSERT INTO cms_portal_menu(name,service_type,index_code) ...
- FutureTask 源码分析
FutureTask 源码分析,这个类的原理与我分析android当中的FutureTask类差不多[http://www.cnblogs.com/daxin/p/3802392.html] publ ...
- windows 下 MySql5.6主从复制
说明: 1.MySql 版本5.6 2.本例中使用的主从服务器ip分别为:192.168.120.211:192.168.120.209 一.配置master服务器 1.配置 在my.ini中[mys ...