簡體   English   中英

C#並行WebClient-操作已超時

[英]C# Parallel WebClient - The operation has timed out

我是一個新手,想知道為什么我在遇到錯誤webclient downloadstring()parallel 我不知是否是因為我的連接緩慢。 這是我的代碼:

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個鏈接時,它停止。

您的連接正在等待先前的連接關閉,因此超時。 到同一主機的並發連接的默認限制為2。在進入Parallel調用之前,請嘗試增加該限制:

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

此處閱讀有關DefaultConnectionLimit更多信息。

適當的價值

類型:System.Int32

ServicePoint對象允許的最大並發連接數。 預設值為2。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM