using System; using System.IO; using System.Net; using System.Text; using System.Text.RegularExpressions; using HtmlAgilityPack; namespace Test.Run { internal class Program { private static void Main(string[] args) { var client = new WebClient(); client.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0"; System.Net.ServicePointManager.DefaultConnectionLimit = 5000; var html = client.DownloadString("http://www.xicidaili.com/nn/5"); var doc = new HtmlDocument(); doc.LoadHtml(html); HtmlNode tbody = doc.GetElementbyId("ip_list"); if (tbody == null) { Console.WriteLine("xpath不正确"); return; } int i = 0; foreach (var tr in tbody.ChildNodes) { if (tr.NodeType == HtmlNodeType.Element) { i++; if (i == 1) { continue; } var ip = tr.ChildNodes[5].InnerText; var port = tr.ChildNodes[7].InnerText; try { var request = (HttpWebRequest) WebRequest.Create("http://1111.ip138.com/ic.asp"); request.KeepAlive = false; request.Method = "get"; request.Timeout = 300; request.ReadWriteTimeout = 300; // request.ServicePoint.ConnectionLimit = 1000; // request.ServicePoint.ConnectionLeaseTimeout = 1000; request.ContentType = "application/json; charset=gb2312"; string address = ip + ":" + port; Console.WriteLine(address); request.Proxy = new WebProxy(address); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("gb2312"))) { string result = sr.ReadToEnd(); response.Close(); Console.WriteLine(result); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } } } }
检测代理是否有效时,设置的过期时间失去作用,控制台一只等待状态。
等待时长明显比设置时长大很多
设置了DefaultConnectionLimit,ServicePoint.ConnectionLimit 等属性还是一样
你这个不清楚没用过,用HttpClient吧,可以设置超时
request.Timeout = 300; request.ReadWriteTimeout = 300;都设置了,也是在GetResponse()之前设置的,你也不是异步调用,说明你的超时设置是正确的,为什么时间不止是5秒,我想最有可能是:1你的DNS的解析时间,不包括在超时里面,2:你var request = (HttpWebRequest) WebRequest.Create("http://1111.ip138.com/ic.asp"),才计算超时时间;
是否可以尝试 请求头中 添加 keepalive的东东