以前帮一个朋友写的“IP采集工具”!
Control.CheckForIllegalCrossThreadCalls = false;
多线程,测试后还比较稳定。
500线程时候,CPU5%左右,内存50多M,1W数据需要6分多钟验证完毕。
以下是部分代码:
public string Getms(string url = "https://www.baidu.com/", string proxy = null)
{
Stopwatch sw = new Stopwatch();
try
{
HttpHelpers helper = new HttpHelpers();
HttpItems items = new HttpItems();
HttpResults hr = new HttpResults();
items.URL = url;
items.ResultType = ResultType.So;
items.ProxyIp = proxy;
sw.Start();
hr = helper.GetHtml(items);
sw.Stop();
if (hr.StatusCode == HttpStatusCode.OK)
{
return sw.ElapsedMilliseconds.ToString();
}
return "-1";
}
catch (Exception)
{
return "-500";
}
}
//打开注册表键
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
int count = ;
while (rk.GetValue("ProxyServer") == null && count != )
{
count = count - ;
//设置代理IP和端口
rk.SetValue("ProxyEnable", );
rk.SetValue("ProxyServer", ip + ":" + port);
}
API.InternetSetOptionA(, , , );