1

I am able to get numbers with enum as suggested by dtb in Getting Http Status code number (200, 301, 404, etc.) from HttpWebRequest and HttpWebResponse. However, for moved permanently site also i am getting 200 (OK). What I want to see is 301 instead. Please help. My code is below. What could be wrong/needs to be corrected?

 public int GetHeaders(string url)
{
    //HttpStatusCode result = default(HttpStatusCode);
    int result = 0;
    var request = HttpWebRequest.Create(url);
    request.Method = "HEAD";
    try
    {
        using (var response = request.GetResponse() as HttpWebResponse)
        {
            if (response != null)
            {
                result = (int)response.StatusCode; // response.StatusCode;
                response.Close();
            }
        }
    }
    catch (WebException we)
    {
        if (we.Response != null)
        {
            result = (int)((HttpWebResponse)we.Response).StatusCode;
        }

    }

    return result;
}

The tool where i am using this code is capable of showing 404, not existing domains but it is ignoring the redirects and shows the details about the redirected url. e.g if i put my older domain easytipsandtricks.com in the text field, it shows the results for tipscow.com (if you check easytipsandtricks.com in any checker tool online, you will notice that it is giving the correct redirect message - 301 Moved). Please help.

Community
  • 1
  • 1
aditya
  • 333
  • 1
  • 6
  • 11

1 Answers1

4

You need to set HttpWebRequest.AllowAutoRedirect to false (default is true) for it to not automatically follow redirects (30x responses).

If AllowAutoRedirect is set to false, all responses with an HTTP status code from 300 to 399 is returned to the application.

Sample:

var request =  (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "HEAD";
request.AllowAutoRedirect = false;
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    Thank you Alexei! However, since HttpWebRequest.Create() returns a WebRequest object, it doesn't have AllowAutoRedirect method. To get it working, it needs to be type cast (HttpWebRequest) so that it is the HttpWebRequest object. Hence, the code is as below: var request = (HttpWebRequest)HttpWebRequest.Create(url); – aditya Jun 19 '13 at 16:51
  • (EDIT: This comment is for a different tool, but uses the similar code) I want to check the google PR of a particular domain but when I enter URL like https://www.facebook.com, i get redirected to "https://www.facebook.com/unsupportedbrowser". I have actually added a condition that if it is a redirect (301 or 302), then not to display the PR rather show a message like how it is seen at http://tools.tipscow.com/check-google-page-rank/?http://www.facebook.com. Any suggestions? – aditya Jun 19 '13 at 19:02
  • 1
    Note for future travelers -- .NET Core ignores AllowAutoRedirect if you get a redirect from HTTPS to HTTP, it will throw an exception instead. – richardtallent Aug 20 '20 at 16:51
  • I got redirected from http to http in core app and get exception too. What's the solution? – Boppity Bop Jan 25 '21 at 16:24