How to get HTML code of a page in C # when headers are set to HTTP / 1.0 404 Not found

Any way to get the html of the webpage even if the title is set to 404? Some pages still have text, and in my case I need to read that text.

Sample C # code for HTML:

 public static string GetHtmlFromUri(string resource)
        {
            string html = string.Empty;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(resource); //Errors here.
            using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
            {
                bool isSuccess = (int)resp.StatusCode < 299 && (int)resp.StatusCode >= 200;
                if (isSuccess)
                {
                    using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
                    {
                        html = reader.ReadToEnd();
                    }
                }
            }
            return html;
        }

And here is the page I created to check this for 404 errors: http://bypass.rd.to/headertest.php
If you look in the header, you will see that it is 404, but the text can be read. Now try to get the page in C # ...

MessageBox.Show(GetHtmlFromUri("http://bypass.rd.to/headertest.php"));

System.Net.WebException
= " : (404) ."
Source = "System"
StackTrace: at System.Net.HttpWebRequest.GetResponse()

+3
1

HttpWebResponse, , . . .

+4

Source: https://habr.com/ru/post/1777027/


All Articles