3

I am a newbie and wanted to know why I am experiencing error on webclient downloadstring() inside the parallel. I am out of nowhere of knowledge whether it is because of the my slow connection. Here is my code:

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);


            } 
     );}

The error occurs in the string html = wc.DownloadString(link);. I see on the output that it stops when it reaches at least 20 links.

4

1 回答 1

2

您的连接正在等待以前的连接关闭,因此超时。与同一主机的并发连接的默认限制是 2。在进入Parallel呼叫之前尝试增加该限制:

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

阅读更多关于DefaultConnectionLimit 这里

适当的价值

类型:System.Int32

ServicePoint 对象允许的最大并发连接数。默认值为 2。

于 2013-10-04T12:33:56.277 回答