我想知道是否有更简单的方法(更简单的方法)来检查500状态代码?
我能想到的唯一方法是这样做:
var statusCodes = new List<HttpStatusCode>()
{
HttpStatusCode.BadGateway,
HttpStatusCode.GatewayTimeout,
HttpStatusCode.HttpVersionNotSupported,
HttpStatusCode.InternalServerError,
HttpStatusCode.NotImplemented,
HttpStatusCode.ServiceUnavailable
};
if (statusCodes.Contains(response.StatusCode))
{
throw new HttpRequestException("Blah");
}
我注意到这些是500种类型:
最佳答案
以5xx开头的状态代码是服务器错误,因此简单的方法是
if ((int)response.StatusCode>=500 && (int)response.StatusCode<600)
throw new HttpRequestException("Server error");