我想知道是否有更简单的方法(更好的方法)来检查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种类型:
答案 0 :(得分:7)
以5xx开头的状态代码是服务器错误,因此简单的方法是
if ((int)response.StatusCode>=500 && (int)response.StatusCode<600)
throw new HttpRequestException("Server error");