/// <summary>
/// 用正则表达式进行取IP
/// </summary>
private void GetIp(string Ips)
{
// string a = textBox1.Text.ToString();
//去IP的正则表达式
string pattern = @"(?<==)[\d\.]+(?=,)";
pattern = @"(\d{1,3}|\*)\.(\d{1,3}|\*)\.(\d{1,3}|\*)\.(\d{1,3}|\*)";
//用正则表达式对字符串进行匹配 然后输出
MatchCollection collection = Regex.Matches(Ips, pattern);
foreach (Match match in collection)
{
Console.WriteLine(match.Value);
}
}
string ips = webBrowser1.Document.Body.InnerHtml.ToString();
GetIp(ips);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AppGetInformation;
using System.IO;
using System.Threading;
using System.Net;
namespace 测试获取代理IP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 验证代理IP
/// </summary>
/// <param name="IP"></param>
private void bbb(object IP)
{
IPSearch Adress = new IPSearch(".\\qqwry.dat");
//string IP="117.21.42.";
// IPSearch ip = new IPSearch(".\\qqwry.dat");
for (int i =0; i < 255; i++)
{
string ips = IP.ToString() + i.ToString();
if (YanzhengIp(ips, 80) == "Error")
{
// Console.WriteLine(i);
// Console.WriteLine(ips);
}
else
{
AppGetInformation.IPSearch.IPLocation pl = new IPSearch.IPLocation();
pl = Adress.GetIPLocation(ips);
File.AppendAllText(".//IP" + IP + ".txt", "(--" + ips + "--)" + pl.country + txtDuan .Text+ Environment.NewLine);
}
}
}
/// <summary>
/// 方法不带参数的线程
/// </summary>
/// <param name="ts"></param>
private void AsynRun(ThreadStart ts)
{
Thread thread = new Thread(ts);
thread.IsBackground = true;
thread.Start();
}
/// <summary>
/// 方法带参数的线程
/// </summary>
/// <param name="ts"></param>
/// <param name="o"></param>
private void AsynRun(ParameterizedThreadStart ts, Object o)
{
Thread thread = new Thread(ts);
thread.IsBackground = true;
thread.Start(o);
}
/// <summary>
/// 验证代理IP是否有效的方法
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
private String YanzhengIp(string IP, int port)
{
bool isok = true;
string rs = null;
try
{
//设置代理IP
WebProxy proxyObject = new WebProxy(IP, port);
//向指定地址发送请求
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://www.baidu.com");
HttpWReq.Proxy = proxyObject;
// HttpWReq.Timeout = 10000;
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
StreamReader sr = new StreamReader(HttpWResp.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
string xmlContent = sr.ReadToEnd().Trim();
sr.Close();
HttpWResp.Close();
HttpWReq.Abort();
rs = xmlContent;
if (HttpWResp.StatusCode == HttpStatusCode.OK && rs.IndexOf("百度一下")>0)
rs = "true";
else
rs = "Error";
//StreamReader sr = new StreamReader(HttpWResp.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
//string xmlContent = sr.ReadToEnd().Trim();
//sr.Close();
//rs = "true";
//isok = false;
}
catch (Exception)
{
//isok = false;
rs = "Error";
}
return rs;
}
private String IPyanzheng(string IP,int port)
{
bool isok = true;
string rs = "";
WebProxy proxyObject = new WebProxy(IP, port);
while (isok)
{
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://www.baidu.com");
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
//HttpWReq.Timeout = 10000;
StreamReader sr = new StreamReader(HttpWResp.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
string xmlContent = sr.ReadToEnd().Trim();
sr.Close();
HttpWResp.Close();
HttpWReq.Abort();
rs = xmlContent;
isok = false;
Console.WriteLine(rs.IndexOf("百度一下"));
}
catch (Exception)
{
isok = false;
rs = "Error";
}
}
return rs;
}
private void button1_Click(object sender, EventArgs e)
{
string ids = "122.125.";
for (int i = 0; i < Convert.ToInt16(txtTimes.Text.ToString());i++ )
{
AsynRun(bbb, ids+i.ToString()+".");
}
}
private void Form1_Load(object sender, EventArgs e)
{
//IPyanzheng("122.115.32.95",80);
//IPSearch Adress = new IPSearch(".\\qqwry.dat");
//AppGetInformation.IPSearch.IPLocation pl = new IPSearch.IPLocation();
//pl = Adress.GetIPLocation("122.115.32.95");
//Console.WriteLine(pl.country);
}
}
}
先采集代理IP然后在对每个IP进行验证
每个端口开一个线程
对所有采集到的IP进行验证
/// 用正则表达式进行取IP
/// </summary>
private void GetIp(string Ips)
{
// string a = textBox1.Text.ToString();
//去IP的正则表达式
string pattern = @"(?<==)[\d\.]+(?=,)";
pattern = @"(\d{1,3}|\*)\.(\d{1,3}|\*)\.(\d{1,3}|\*)\.(\d{1,3}|\*)";
//用正则表达式对字符串进行匹配 然后输出
MatchCollection collection = Regex.Matches(Ips, pattern);
foreach (Match match in collection)
{
Console.WriteLine(match.Value);
}
}
string ips = webBrowser1.Document.Body.InnerHtml.ToString();
GetIp(ips);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AppGetInformation;
using System.IO;
using System.Threading;
using System.Net;
namespace 测试获取代理IP
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 验证代理IP
/// </summary>
/// <param name="IP"></param>
private void bbb(object IP)
{
IPSearch Adress = new IPSearch(".\\qqwry.dat");
//string IP="117.21.42.";
// IPSearch ip = new IPSearch(".\\qqwry.dat");
for (int i =0; i < 255; i++)
{
string ips = IP.ToString() + i.ToString();
if (YanzhengIp(ips, 80) == "Error")
{
// Console.WriteLine(i);
// Console.WriteLine(ips);
}
else
{
AppGetInformation.IPSearch.IPLocation pl = new IPSearch.IPLocation();
pl = Adress.GetIPLocation(ips);
File.AppendAllText(".//IP" + IP + ".txt", "(--" + ips + "--)" + pl.country + txtDuan .Text+ Environment.NewLine);
}
}
}
/// <summary>
/// 方法不带参数的线程
/// </summary>
/// <param name="ts"></param>
private void AsynRun(ThreadStart ts)
{
Thread thread = new Thread(ts);
thread.IsBackground = true;
thread.Start();
}
/// <summary>
/// 方法带参数的线程
/// </summary>
/// <param name="ts"></param>
/// <param name="o"></param>
private void AsynRun(ParameterizedThreadStart ts, Object o)
{
Thread thread = new Thread(ts);
thread.IsBackground = true;
thread.Start(o);
}
/// <summary>
/// 验证代理IP是否有效的方法
/// </summary>
/// <param name="IP"></param>
/// <param name="port"></param>
/// <returns></returns>
private String YanzhengIp(string IP, int port)
{
bool isok = true;
string rs = null;
try
{
//设置代理IP
WebProxy proxyObject = new WebProxy(IP, port);
//向指定地址发送请求
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://www.baidu.com");
HttpWReq.Proxy = proxyObject;
// HttpWReq.Timeout = 10000;
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
StreamReader sr = new StreamReader(HttpWResp.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
string xmlContent = sr.ReadToEnd().Trim();
sr.Close();
HttpWResp.Close();
HttpWReq.Abort();
rs = xmlContent;
if (HttpWResp.StatusCode == HttpStatusCode.OK && rs.IndexOf("百度一下")>0)
rs = "true";
else
rs = "Error";
//StreamReader sr = new StreamReader(HttpWResp.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
//string xmlContent = sr.ReadToEnd().Trim();
//sr.Close();
//rs = "true";
//isok = false;
}
catch (Exception)
{
//isok = false;
rs = "Error";
}
return rs;
}
private String IPyanzheng(string IP,int port)
{
bool isok = true;
string rs = "";
WebProxy proxyObject = new WebProxy(IP, port);
while (isok)
{
try
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://www.baidu.com");
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
//HttpWReq.Timeout = 10000;
StreamReader sr = new StreamReader(HttpWResp.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
string xmlContent = sr.ReadToEnd().Trim();
sr.Close();
HttpWResp.Close();
HttpWReq.Abort();
rs = xmlContent;
isok = false;
Console.WriteLine(rs.IndexOf("百度一下"));
}
catch (Exception)
{
isok = false;
rs = "Error";
}
}
return rs;
}
private void button1_Click(object sender, EventArgs e)
{
string ids = "122.125.";
for (int i = 0; i < Convert.ToInt16(txtTimes.Text.ToString());i++ )
{
AsynRun(bbb, ids+i.ToString()+".");
}
}
private void Form1_Load(object sender, EventArgs e)
{
//IPyanzheng("122.115.32.95",80);
//IPSearch Adress = new IPSearch(".\\qqwry.dat");
//AppGetInformation.IPSearch.IPLocation pl = new IPSearch.IPLocation();
//pl = Adress.GetIPLocation("122.115.32.95");
//Console.WriteLine(pl.country);
}
}
}
先采集代理IP然后在对每个IP进行验证
每个端口开一个线程
对所有采集到的IP进行验证