Question

I want to check if a particular page gets redirected or not. However, whenever I try this the headers I get back seem to be from the redirected page, not the initially requested page (and, in particular, the status is OK rather than the 302 I want to see).

Is there something I can set so that it won't automatically follow the redirects?

WebRequest request = WebRequest.Create(@"http://www.example.com/page.html");
request.Method = "HEAD";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.Redirect)
{
    ...
}
else
{
    MessageBox.Show("HTTP Code: " + response.StatusCode + "\r\n\r\n" + response.StatusDescription);
    return false;
}
Was it helpful?

Solution

HttpWebRequest.AllowAutoRedirect

Gets or sets a value that indicates whether the request should follow redirection responses.
...
If AllowAutoRedirect is set to false, all responses with an HTTP status code from 300 to 399 is returned to the application.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top