سؤال

When I load http://support.microsoft.com/common/survey.aspx?scid=sw%3ben%3b3547&showpage=1 thru a browser I am getting to the correct page but when I do the following HttpWebResponse returns a different uri which does not exist. Help Please!!!

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;
}
هل كانت مفيدة؟

المحلول

You have to use some user agent they can use, for example:

request.UserAgent = "Mozilla";
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top