public void Execute(PerformContext context) { context.WriteLine("开始验证代理Ip的有效性"); var ips = Task.Run(async() => await _proxyIpService.GetWaitValidateIps(20)) .ConfigureAwait(false) .GetAwaiter() .GetResult(); var total = ips.Count; var successCount = 0; var http = new HttpRequestProvider(); foreach (var proxyIpDto in ips) { var success = false; try { var result = http.Get("http://www.jucheap.com/home/showip", null, proxyIpDto.Ip, proxyIpDto.Port); success = "ok".Equals(result, StringComparison.OrdinalIgnoreCase); } catch (Exception ex) { context.WriteLine($"验证失败:{ex.Message}"); } if (success) { successCount++; } Task.Run(async() => await _proxyIpService.Update(proxyIpDto.Id, success)).ConfigureAwait(false); } context.WriteLine($"验证完成,总共{total}条,成功{successCount}条"); context.WriteLine("验证执行完成"); }
private List <ProxyIpDto> GrabWest(PerformContext context) { var proxyIp = new List <ProxyIpDto>(); try { context.WriteLine("->西刺"); var http = new HttpRequestProvider(); var url = "http://www.xicidaili.com/nn/1"; // 西刺 var result = http.Get(url); if (result.IsNotBlank()) { var count = 0; var regex = @"<td>(\d+\.\d+\.\d+\.\d+)</td>\s+<td>(\d+)</td>"; var mstr = Regex.Match(result, regex); while (mstr.Success) { var ip = new ProxyIpDto { Ip = mstr.Groups[1].Value, Port = mstr.Groups[2].Value.ToInt() }; proxyIp.Add(ip); mstr = mstr.NextMatch(); count++; } context.WriteLine($"->西刺,抓取到{count}条"); } } catch (Exception ex) { context.WriteLine($"->西刺,发生错误:{ex.Message}"); Log.Error(ex); } return(proxyIp); }
private List <ProxyIpDto> Grab66(PerformContext context) { var http = new HttpRequestProvider(); var proxyIp = new List <ProxyIpDto>(); context.WriteLine("->66Ip"); try { var url = "http://www.66ip.cn/nmtq.php?getnum=100&isp=0&anonymoustype=3&start=&ports=&export=&ipaddress=&area=1&proxytype=2&api=66ip"; // 66ip var result = http.Get(url); if (result.IsNotBlank()) { var count = 0; var regex = @"(\d+\.\d+\.\d+\.\d+):(\d+)"; var mstr = Regex.Match(result, regex); while (mstr.Success) { var ip = new ProxyIpDto { Ip = mstr.Groups[1].Value, Port = mstr.Groups[2].Value.ToInt() }; proxyIp.Add(ip); mstr = mstr.NextMatch(); count++; } context.WriteLine($"->66Ip,抓取到{count}条"); } } catch (Exception ex) { context.WriteLine($"->66Ip,发生错误:{ex.Message}"); Log.Error(ex); } return(proxyIp); }