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

Posted by James Lawruk on Stack Overflow See other posts from Stack Overflow or by James Lawruk
Published on 2009-08-25T20:39:29Z Indexed on 2011/01/15 14:53 UTC
Read the original article Hit count: 229

Filed under:
|
|
|

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());

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET