How to properly check http status code for HttpResponseMessage in C#?

Viewed 1756

complete novice at C# reporting in

Lets say I have the following code

HttpResponseMessage response = ...

How would I check if the status code for response is a 403?

The property StatusCode is an object - as opposed to an integer, So I am not quite sure what to do.

1 Answers

You can either use the HttpStatusCode enum or cast the enum to an integer:

if (response.StatusCode == HttpStatusCode.Forbidden)
{
   ...
}

or

if ((int)response.StatusCode == 403)
{
   ...
}
Related