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. vue2.0如何自定义全局变量的方法

    方法一:http://www.jianshu.com/p/04dffe7a6b74 //在mian.js中写入函数 Vue.prototype.changeData = function (){ al ...

  2. Linux环境变量相关总结

    Linux下环境变量分为全局和局部变量两种. Linux 下通常提供三种工具可以可以查看环境变量:printenv.env.set.前两个可以查看全局环境变量,最后一个会打印包含全局和局部环境变量. ...

  3. vi/vim使用进阶: 文件浏览和缓冲区浏览

    原文:https://blog.easwy.com/archives/advanced-vim-skills-netrw-bufexplorer-winmanager-plugin/ 本节所用命令的帮 ...

  4. python各类项目模块记录

    看看下面这些项目,并试着用它们实现一些东西出来:• The Django Tutorial 试着用 Django Web Framework 创建一个 web 应用.• SciPy 如果你对科学,数学 ...

  5. java基础篇---网络编程(IP与URL)

    一:IP与InetAddress 在Java中支持网络通讯程序的开发,主要提供了两种通讯协议:TCP协议,UDP协议 可靠地连接传输,使用三方握手的方式完成通讯 不可靠的连接传输,传输的时候接受方不一 ...

  6. 【转】 Java中的变量赋值和参数传递

    原文地址:http://blog.csdn.net/whmii/article/details/3363667 变量赋值和参数传递是java中两个容易让人迷惑的问题.对于原始类型(primitives ...

  7. docker探索-docker私有仓库搭建(九)

    前言 本文讲解搭建docker的私有仓库,和Mavan的管理一样,Dockers不仅提供了一个中央仓库,同时也允许我们使用registry搭建本地私有仓库 一.环境 ip 主机名 操作系统 角色 19 ...

  8. Py2.7 no module named tkinter

    一个简单的例子 #! /usr/bin/env python#coding=utf-8from tkinter import *Label(text="Spam").pack()m ...

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

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

  10. OpenWRT中的按键和灯的GPIO控制实现_转

    本文转自:OpenWRT中的按键和灯的GPIO控制实现 基于BarrierBreaker版本,基于AR9331 AP121 Demo单板 来进行描述 1.灯 A.在mach-ap121.c中,定义了灯 ...