SeyoS
SeyoS

Reputation: 681

How to handle custom HTTP status code with HttpWebResponse

I try to handle custom HTTP status code like 444 or 429 that are not in the HttpStatusCode enum when receving them with a WebException ( (ex.Response as HttpWebResponse).StatusCode ).

Is there a better way than parsing response as suggested in this answer HttpWebResponse Status Code 429 ?

As there are many of them, this solution is not suitable.

Upvotes: 0

Views: 707

Answers (1)

Alberto
Alberto

Reputation: 15951

Just cast the StatusCode to int:

int code = (int)((ex.Response as HttpWebResponse).StatusCode);
if(code == 429)
{
    ...
}

this will work even if there isn't a corresponding enum value because in C# enums are not checked.

Upvotes: 1

Related Questions