CSharp - HTMLAgilityPack ASP.NET C# 错误处理


 Uri url3 = new Uri("http://www.bigpara.com/borsa/gunun-ozeti/");
 WebClient client3 = new WebClient();
 string html3 = client3.DownloadString(url3);
 HtmlAgilityPack.HtmlDocument dokuman3 = new HtmlAgilityPack.HtmlDocument();
 dokuman3.LoadHtml(html3);

错误消息为:

System.NullReferenceException and the remote server isn't resolved




如何防止这个错误?

时间:

如果使用HtmlWeb对象,它允许你指定额外的参数来解析HTML,这里使用AutoDetectEncoding。


Uri url3 = new Uri("http://www.bigpara.com/borsa/gunun-ozeti/");
HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
web.AutoDetectEncoding = true;
HtmlAgilityPack.HtmlDocument dokuman3 = web.Load(url3.AbsoluteUri);

希望有帮助!

...