2013-10-03 4 views
2

저는 초보자이며 parallel 안에 webclient downloadstring()에 대한 오류가 발생하는 이유를 알고 싶습니다. 나는 느린 연결 때문인지 여부에 관계없이 지식이 없습니다. 여기 내 코드 :C# 병렬 웹 클라이언트 - 작업 시간이 초과되었습니다.

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

"작업 시간 초과"웹 클라이언트를 가리키는 오류입니다. downloadString() –

+0

병렬 라이브러리를 사용하여이 작업을 수행 했습니까? – usefulBee

답변

1

이전 연결이 닫히기를 기다리는 연결이므로 시간이 초과되었습니다.

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

DefaultConnectionLimit here에 대해 자세히 알아보기 : 동일한 호스트에 동시 연결에 대한 기본 제한은 Parallel 전화를 입력하기 전에 그 한계를 증가 2. 시도이다.

속성 값

유형 : 선택 System.Int32

ServicePoint 개체에서 허용하는 동시 연결의 최대 수. 기본값은 2입니다.

+0

그러나 그것은 나에게 효과가없는 것 같습니다. –

관련 문제