我在我的asp.net网站上打开一个谷歌页面,页面打开成功,但每当我尝试与该页面进行交互时,我收到404错误,如果我尝试搜索404错误即将到来我的代码如下。
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.google.com.pk/");
myHttpWebRequest.MaximumAutomaticRedirections = 1;
myHttpWebRequest.AllowAutoRedirect = true;
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
string tempProviderString = null;
using (StreamReader sr = new StreamReader(myHttpWebResponse.GetResponseStream(), System.Text.Encoding.UTF8))
{
tempProviderString = sr.ReadToEnd();
}
Response.Write(tempProviderString);
答案 0 :(得分:0)
首先处理您的404错误。
try
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://www.google.com.pk/");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
}
catch(WebException ex)
{
HttpWebResponse webResponse = (HttpWebResponse)ex.Response;
if (webResponse.StatusCode == HttpStatusCode.NotFound)
{
404 error....
}
}