当标题设置为HTTP/1.0 404未找到时,如何在C#中获取页面的HTML
有什么方法可以获取网页的html,即使标题设置为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
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"));