HttpWebResponse重定向到错误页面

本文关键字:错误 重定向 HttpWebResponse | 更新日期: 2023-09-27 17:59:43

加载时http://support.microsoft.com/common/survey.aspx?scid=sw%3ben%3b3547&showpage=1通过浏览器,我到达了正确的页面,但当我执行以下HttpWebResponse时,返回了一个不存在的不同uri。救命!!!

private string[] getTitleNewUrl()
{
    string[] titleNewUrl = new string[2];
    var navigatedUrl = string.Empty;
    string title = string.Empty;
    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://support.microsoft.com/common/survey.aspx?scid=sw%3ben%3b3547&showpage=1");
        request.Credentials = System.Net.CredentialCache.DefaultCredentials;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            navigatedUrl = response.ResponseUri.ToString(); **//this returns [http://support.microsoft.com/default.aspx?scid=gp;en-us;fmserror][1]**
            StreamReader sr = new StreamReader(response.GetResponseStream());
            var htmlSource = sr.ReadToEnd();
            Match m = Regex.Match(htmlSource, @"<title>'s*(.+?)'s*</title>");
            if (m.Success)
            {
                title = m.Groups[1].Value;
            }
            titleNewUrl[0] = title;
            titleNewUrl[1] = navigatedUrl;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Invalid URL: " + navigatedUrl + " Error: " + ex.Message);
    }
    return titleNewUrl;
}

HttpWebResponse重定向到错误页面

您必须使用他们可以使用的一些用户代理,例如:

request.UserAgent = "Mozilla";