HtmlAgilityPack.dll中發生“System.StackOverflowException”

asp.net c# dll html-agility-pack

我試圖使用HtmlAgilityPack.dll廢棄頁面,但是一些url進入函數,我收到了錯誤,我無法在try-catch塊中捕獲它。所以有人可以幫助我嗎?

錯誤:HtmlAgilityPack.dll中出現未處理的“System.StackOverflowException”類型異常

public void HtmlLoad(string url)
    {
        try
        {
            HttpWebRequest myHttpWebRequest = null;     //Declare an HTTP-specific implementation of the WebRequest class.
            HttpWebResponse myHttpWebResponse = null;   //Declare an HTTP-specific implementation of the WebResponse class
            //Create Request  // 
            myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            myHttpWebRequest.Method = "GET";
            myHttpWebRequest.ContentType = "text/html; encoding='utf-8'";
            //Get Response
            myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

            Stream data = myHttpWebResponse.GetResponseStream();//client.OpenRead(url);
            doc.Load(data);
            data.Close();

        }
        catch (Exception ex) { throw ex; }
    }

熱門答案

你可以嘗試這個乾淨

 public static async Task<int> HtmlLoadAsync(string url/*, bool addUserAgent = false*/)
    {
        try
        {
            var client = new HttpClient();
            //if (addUserAgent)                 OPTIONAL 
            //{
            //    client.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
            //}
            //client.Timeout = TimeOut;
            var response = client.GetStringAsync(url);
            var urlContents = await response;
            var document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(urlContents);
            // process document now
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return 0;
    }

現在打電話給它

private async void Process()
{
  await HtmlLoadAsync("http://....");
}


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow