我是新手,想知道为什么我在parallel
内部遇到webclient downloadstring()
错误。无论是因为我的连接速度缓慢,我都无从得知。这里是我的代码:C#并行WebClient - 操作已超时
for (int i = 2; i <= 5; i++)
{
string ebayLink = "http://www.ebay.de/sch/Studium-Wissen-/1105/i.html?LH_Auction=1&_sop=1&_nkw=&_pgn=" + i;
//string ebayLink = "http://www.ebay.de/sch/Schule-Ausbildung-/40434/i.html?LH_Auction=1&_sop=1&_nkw=&_pgn=" + i;
ebayLink = "http://www.ebay.de/sch/i.html?LH_Auction=1&_sacat=0&_from=R40&_nkw=B%C3%BCcher&_sop=1&_pgn=" + i;
HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = hw.Load(ebayLink);
List<string> eanList = new List<string>();
List<string> links = new List<string>();
foreach (var link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
string url = link.GetAttributeValue("href", "");
if (url.Contains(".de/itm") && !links.Contains(url) && !url.Contains("pt=Zeitschriften") && !url.Contains("pt=Belletristik"))
{
links.Add(url);
}
}
Parallel.ForEach(links, link =>
{
WebClient wc = new WebClient();
string html = wc.DownloadString(link);
EbayItem ebayItem = new EbayItem(html);
string ean = ebayItem.ean;
string amazonUsedPrice = string.Empty;
amazonUsedPrice = getAmazonUsedPrice(ean);
Product product = new Product();
product.EbayUrl = link;
product.Ean = ean;
product.AmazonPriceString = amazonUsedPrice;
product.ebayItem = ebayItem;
productList.Add(product);
}
);}
错误发生在string html = wc.DownloadString(link);
。当输出到达至少20个链接时,我会在输出中看到它停止。
什么是错误? – zimdanen
“操作已超时”,这是指向web客户端的错误downloadString() –
您是否碰巧使用并行库来实现这一功能? – usefulBee