20550

Forcing C#'s HTTP Response to Return a Status Code Instead of a Description

I am currently using this script to get HTTP response headers.

public static List<string> GetHttpResponseHeaders(string url) { List<string> headers = new List<string>(); WebRequest webRequest = HttpWebRequest.Create(url); using (WebResponse webResponse = webRequest.GetResponse()) { headers.Add("Status Code: " + (int) ((HttpWebResponse) webResponse).StatusCode); } return headers; }

Specifically, Status Code: is what I am interested in. With that said, it appears that StatusCode() doesn't actually return a "status code," and on successful requests, it only returns an OK instead of a 200.

Is there a way to force it to return the actual code instead of a description?

Answer1:

With that said, it appears that StatusCode() doesn't actually return a "status code," and on successful requests, it only returns an OK instead of a 200.

No, it returns an HttpStatusCode enum value. If you call ToString on an enum value that has a name, it will return the name.

The simplest way of avoiding that is just to cast it to int:

headers.Add("Status Code: " + (int) ((HttpWebResponse) webResponse).StatusCode);

Or to make the rest of the block cleaner, cast the response once:

using (WebResponse webResponse = webRequest.GetResponse()) { var httpResponse = (HttpWebResponse) webResponse; headers.Add("URL: " + url); headers.Add("Status Code: " + (int) httpResponse.StatusCode); headers.Add("Status Description: " + httpResponse.StatusDescription + "\n"); }

(Note that when you're using string concatenation, ToString will be called implicitly if necessary - and it's never worth calling on something like StatusDescription which is already a string.)

Answer2:

StatusCode is an HttpStatusCode enum.

You can cast it to int to get the underlying value.

Answer3:

The reason you're seeing the value "OK" is because property HttpWebResponse.StatusCode is of type HttpStatusCode - an enumeration. Calling .ToString() on an enumeration will give you text, not a number.

So, while you're not seeing the numeric value of the status code, you are seeing a representation of the status code, and not the status description.

If you need the integer value, you can cast the enum to int (HttpStatusCode.OK does indeed have a value of 200) - but it's worth considering that there's no guarantee that the integer values of the enum will line up with the status code values.

Recommend

  • HttpWebResonse hangs on multiple request
  • Application hangs on GetRequestStream() after first request
  • HttpWebRequest redirect with cookies
  • How to test method Jsoup.connect (static) using PowerMock?
  • C# helper classes to implement NTLM authorization
  • How to make a user wait with Laravel
  • Where in the relevant specification is it documented that some comments in a SQL script are, in fact
  • Ability to use Bootstrap 3 grid system to specify width of select element
  • Generating anchors with PyYAML.dump()?
  • CSS how to fix an element to scroll horizontally with the page but not vertically?
  • Get Currently Active User in Android
  • How to Save JSON data to SQL server database in C#?
  • How do I Dispose a HttpResponseMessage in my Web Api Method?
  • Can my PDF ping my server when it is opened?
  • How to retrieve information from antrun back to maven?
  • Can't remove headers after they are sent
  • How to use JavaScript to determine whether a file exists in a directory?
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Compare two NSDates in iPhone
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • How can I remove ASP.NET Designer.cs files?
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?