HttpStatusCode是任何500类型

时间:2013-09-26 08:59:21

标签: c# http-status-codes

我想知道是否有更简单的方法(更好的方法)来检查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种类型:

  • BadGateway
  • GatewayTimeout
  • HttpVersionNotSupported
  • InternalServerError
  • NotImplemented
  • ServiceUnavailable

1 个答案:

答案 0 :(得分:7)

以5xx开头的状态代码是服务器错误,因此简单的方法是

if ((int)response.StatusCode>=500 && (int)response.StatusCode<600)
      throw new HttpRequestException("Server error");