Getting Http Status code number (200, 301, 404, etc.) from HttpWebRequest and HttpWebResponse

338,079

Solution 1

Console.Write((int)response.StatusCode);

HttpStatusCode (the type of response.StatusCode) is an enumeration where the values of the members match the HTTP status codes, e.g.

public enum HttpStatusCode
{
    ...
    Moved = 301,
    OK = 200,
    Redirect = 302,
    ...
}

Solution 2

You have to be careful, server responses in the range of 4xx and 5xx throw a WebException. You need to catch it, and then get status code from a WebException object:

try
{
    wResp = (HttpWebResponse)wReq.GetResponse();
    wRespStatusCode = wResp.StatusCode;
}
catch (WebException we)
{
    wRespStatusCode = ((HttpWebResponse)we.Response).StatusCode;
}

Solution 3

As per 'dtb' you need to use HttpStatusCode, but following 'zeldi' you need to be extra careful with code responses >= 400.

This has worked for me:

HttpWebResponse response = null;
HttpStatusCode statusCode;
try
{
    response = (HttpWebResponse)request.GetResponse();
}
catch (WebException we)
{
    response = (HttpWebResponse)we.Response;
}

statusCode = response.StatusCode;
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
sResponse = reader.ReadToEnd();
Console.WriteLine(sResponse);
Console.WriteLine("Response Code: " + (int)statusCode + " - " + statusCode.ToString());

Solution 4

Just coerce the StatusCode to int.

var statusNumber;
try {
   response = (HttpWebResponse)request.GetResponse();
   // This will have statii from 200 to 30x
   statusNumber = (int)response.StatusCode;
}
catch (WebException we) {
    // Statii 400 to 50x will be here
    statusNumber = (int)we.Response.StatusCode;
}

Solution 5

//Response being your httpwebresponse
Dim str_StatusCode as String = CInt(Response.StatusCode)
Console.Writeline(str_StatusCode)
Share:
338,079
Jsandesu
Author by

Jsandesu

Husband, Father, Catholic, Programmer Currently a Developer at GeoDecisions Besides programming, I also enjoy running, hiking, reading, golf, and spending time with my family. CV: careers.stackoverflow.com/lawruk Personal Web Site: www.lawruk.com

Updated on January 19, 2020

Comments

  • Jsandesu
    Jsandesu over 4 years

    I am trying to get the HTTP status code number from the HttpWebResponse object returned from a HttpWebRequest. I was hoping to get the actual numbers (200, 301,302, 404, etc.) rather than the text description. ("Ok", "MovedPermanently", etc.) Is the number buried in a property somewhere in the response object? Any ideas other than creating a big switch function? Thanks.

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest
                                               .Create("http://www.gooogle.com/");
    webRequest.AllowAutoRedirect = false;
    HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
    //Returns "MovedPermanently", not 301 which is what I want.
    Console.Write(response.StatusCode.ToString());