I know I can just typecast the result from GetHttpCode() to HttpStatusCode, but I don't like to typecast enums. Especially because the MSDN doc doesn't explicitely say that the values of the enums are always the exact corresponding http status code.
I feel that GetHttpCode() should just return HttpStatusCode.
Should I just stop complaining and typecast?
catch(HttpException ex)
{
switch((HttpStatusCode)ex.GetHttpCode())
{
case HttpStatusCode.NotFound:
// Do stuff...
break;
// More cases...
}
}
EDIT: note that HttpWebResponse.StatusCode is of type HttpStatusCode
It's worth noting that HTTP permits custom status codes, so it's not guaranteed that the enum will contain the value from your response. For this reason, it's sensible for the API to return the int
value instead of trying to use the enum.
HTTP status codes don't have to come from a fixed list, though they usually do. Using an enum is a convenience. Requiring an enum would be wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With