简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM