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
intvalue instead of trying to use the enum.