Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当标题设置为HTTP/1.0 404未找到时,如何在C#中获取页面的HTML_C#_Html_Httpwebrequest_Http Headers_Http Status Code 404 - Fatal编程技术网

当标题设置为HTTP/1.0 404未找到时,如何在C#中获取页面的HTML

当标题设置为HTTP/1.0 404未找到时,如何在C#中获取页面的HTML,c#,html,httpwebrequest,http-headers,http-status-code-404,C#,Html,Httpwebrequest,Http Headers,Http Status Code 404,有什么方法可以获取网页的html,即使标题设置为404?有些页面上仍然有文本,在我的情况下,我需要阅读这些文本 示例C#获取HTML的代码: public static string GetHtmlFromUri(string resource) { string html = string.Empty; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(resource

有什么方法可以获取网页的html,即使标题设置为404?有些页面上仍然有文本,在我的情况下,我需要阅读这些文本

示例C#获取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;
        }
System.Net.WebException未处理
Message=“远程服务器返回错误:(404)未找到。”
Source=“System”
StackTrace:at System.Net.HttpWebRequest.GetResponse()上


异常包含
HttpWebResponse
,您可以从中访问发送回的所有内容。有关示例,请参见

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