2013-10-03 62 views
2

我是新手,想知道为什么我在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个链接时,我会在输出中看到它停止。

+0

什么是错误? – zimdanen

+0

“操作已超时”,这是指向web客户端的错误downloadString() –

+0

您是否碰巧使用并行库来实现这一功能? – usefulBee

回答

1

您的连接正在等待先前的连接关闭,因此超时。对于在同一主机的并发连接的缺省限制是2尝试输入您Parallel调用之前增加了限制:

System.Net.ServicePointManager.DefaultConnectionLimit = int.MaxValue; 

了解更多关于DefaultConnectionLimithere

属性值

类型:System.Int32

通过的ServicePoint对象允许的并发连接的最大数目。默认值是2.

+0

但它似乎不适合我。 –