简体   繁体   中英

C# System.NullReferenceException NSoup

for (int x = 0; x < 50; x++)
{
    NSoupClient.Connect("https://steamcommunity.com/gid/" + x)
               .UserAgent("Firefox")
               .Timeout(10000)
               .Get();
}

This piece of code will throw me this error (and it always stops when the int is 5):

An unhandled exception of type 'System.NullReferenceException' occurred in NSoup.dll

在此输入图像描述

Seems You are not the only having the issue - https://nsoup.codeplex.com/workitem/8

Seems as an issue with library itself.

I fixed this using a WebClient to download the page.

using (WebClient wc = new WebClient())
{
    wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows; Windows NT 5.1; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4");
    string html = wc.DownloadString(url);
    Document document = NSoupClient.Parse(html);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM