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 ...
随机推荐
- Spark中的IsNotNull函数怎么用
Spark中的IsNotNull函数怎么用 在这里看到的这个函数,就是判断是否为空,但是开始不知道怎么用,后来找到了,要在View中用,也就是SparkSQL中.如下: spark.sql(" ...
- c++ primer读书笔记之c++11(三)
1 =default构造函数限定符 c++11针对构造函数提供了=default限定符,可以用于显式指定编译器自动生成特定的构造函数.析构或赋值运算函数.参考代码如下: class CtorDftTy ...
- RIFF格式简介
Resource Interchange File Format(简称RIFF),资源交换文件格式,是一种按照标记区块存储数据(tagged chunks)的通用文件存储格式,多用于存储音频.视频等多 ...
- iOS高版本备份恢复到低版本系统的方法
一般来说,在更新iOS系统的时候我们都会建议大家先用iTunes对系统进行完整备份.但时不时都会有人偷懒,或者使用手机OTA升级而没有对系统备份,最终导致不满意新系统了,想降级却无备份可以恢复的尴尬局 ...
- Leetcode: Remove Duplicates from Sorted List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- Pyperclip – A cross-platform clipboard module for Python
Usage is simple: import pyperclip pyperclip.copy('The text to be copied to the clipboard.') spam = p ...
- JAVA-JSP内置对象之request获得所有的参数名称
相关资料:<21天学通Java Web开发> 获得所有的参数名称1.request对象的getParameterNames()方法可以获得所有的参数名称. RequestForm2.jsp ...
- PHP 图片处理类 错误处理方法:
call an undefined function exif_imagetype() 打开扩展php.ini 将 ; 去掉: extension=php_exif.dll 并将extension=p ...
- 基于CSS3鼠标滑过放大突出效果
还记得之前分享过一款CSS3图片悬停放大特效,效果非常不错.今天我们要再来分享一款类似的CSS鼠标滑过放大突出效果,只不过之前那个是图片,这次是色块,其实掌握了其CSS原理,任何网页元素都可以实现这种 ...
- 在分布式系统里看CAP定理
本文转自:http://zhuanlan.51cto.com/art/201703/534587.htm 计算机界有很多高大上又难于理解的术语,CAP就是其中之一, 什么一致性(Consistency ...