Web请求打开另一个网站页面,但与该页面的交互给出错误

时间:2016-06-21 05:20:17

标签: asp.net

我在我的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);

1 个答案:

答案 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....
            }
        }