【发布时间】:2023-03-03 11:33:01
【问题描述】:
基本上,我想从同一个 URL 访问很多页面,但我不想在同一秒发送垃圾邮件。而且我不想在 30 分钟内结束。 并行内的 Thread.Sleep() 工作正常吗? 因为大约 90 分钟后我才完成。
var pages = new List<string>( urls );
var sources = new BlockingCollection<string>();
string htmlCode = "";
Random randomN = new Random();
Parallel.ForEach(pages, x =>
{
x = x.Replace("//", "http://");
int sleep = randomN.Next(0, 1800000);
Thread.Sleep(sleep);
HttpWebResponse response = null;
CookieContainer cookie = new CookieContainer();
HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create(x);
newRequest.Timeout = 15000;
newRequest.Proxy = null;
newRequest.CookieContainer = cookie;
newRequest.UserAgent = "Mozilla / 5.0(Windows NT 5.1) AppleWebKit / 537.11(KHTML, like Gecko) Chrome / 23.0.1300.0 Iron / 23.0.1300.0 Safari / 537.11";
newRequest.ContentLength = 0;
response = (HttpWebResponse)newRequest.GetResponse();
htmlCode = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(1252)).ReadToEnd();
sources.Add(htmlCode);
});
return sources;
【问题讨论】:
标签: multithreading