HTTP Status Codes in C#

I’m in the middle of writing ab WebDAV HttpHandler. Strangely, I could not find a list of HTTP Status Code constants in C# on the web, and transformed the codes from the RFCs myself.

I chose to distinguish between HTTP codes and WebDAV codes. Since WebDAV sits on top of HTTP, they share some codes, so I derived the WebdavStatusCode class from the HttpStatusCode class.

Here they are:

// http://devio.wordpress.com/2007/11/14/http-status-codes-in-c
public class HttpStatusCode
{
  protected HttpStatusCode() {}

  // http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1.1

  public const int Continue = 100;        // Section 10.1.1: Continue
  public const int SwitchingProtocols = 101;        // Section 10.1.2: Switching Protocols

  public const int OK = 200;                // Section 10.2.1: OK
  public const int Created = 201;        // Section 10.2.2: Created
  public const int Accepted =  202;        // Section 10.2.3: Accepted
  public const int NonAuthoritativeInformation = 203;    // Section 10.2.4: Non-Authoritative Information
  public const int NoContent =  204;        // Section 10.2.5: No Content
  public const int ResetContent = 205;    // Section 10.2.6: Reset Content
  public const int PartialContent = 206;    // Section 10.2.7: Partial Content

  public const int MultipleChoices = 300;        // Section 10.3.1: Multiple Choices
  public const int MovedPermanently =  301;        // Section 10.3.2: Moved Permanently
  public const int Found =  302;            // Section 10.3.3: Found
  public const int SeeOther = 303;        // Section 10.3.4: See Other
  public const int NotModified =  304;    // Section 10.3.5: Not Modified
  public const int UseProxy =  305;        // Section 10.3.6: Use Proxy
  public const int TemporaryRedirect = 307;        // Section 10.3.8: Temporary Redirect

  public const int BadRequest = 400;        // Section 10.4.1: Bad Request
  public const int Unauthorized = 401;    // Section 10.4.2: Unauthorized
  public const int PaymentRequired =  402;        // Section 10.4.3: Payment Required
  public const int Forbidden = 403;        // Section 10.4.4: Forbidden
  public const int NotFound = 404;        // Section 10.4.5: Not Found
  public const int MethodNotAllowed = 405;        // Section 10.4.6: Method Not Allowed
  public const int NotAcceptable = 406;    // Section 10.4.7: Not Acceptable
  public const int ProxyAuthenticationRequired = 407;    // Section 10.4.8: Proxy Authentication Required
  public const int RequestTimeout = 408;    // Section 10.4.9: Request Time-out
  public const int Conflict = 409;        // Section 10.4.10: Conflict
  public const int Gone =  410;            // Section 10.4.11: Gone
  public const int LengthRequired = 411;    // Section 10.4.12: Length Required
  public const int PreconditionFailed =  412;    // Section 10.4.13: Precondition Failed
  public const int RequestEntityTooLarge =  413;    // Section 10.4.14: Request Entity Too Large
  public const int RequestUriTooLarge =  414;    // Section 10.4.15: Request-URI Too Large
  public const int UnsupportedMediaType =  415;    // Section 10.4.16: Unsupported Media Type
  public const int RequestedRangeNotSatisfiable = 416;    // Section 10.4.17: Requested range not satisfiable
  public const int ExpectationFailed = 417;        // Section 10.4.18: Expectation Failed

  public const int InternalServerError =  500;    // Section 10.5.1: Internal Server Error
  public const int NotImplemented = 501;    // Section 10.5.2: Not Implemented
  public const int BadGateway = 502;        // Section 10.5.3: Bad Gateway
  public const int ServiceUnavailable =  503;    // Section 10.5.4: Service Unavailable
  public const int GatewayTimeout =  504;        // Section 10.5.5: Gateway Time-out
  public const int HttpVersionNotSupported = 505;        // Section 10.5.6: HTTP Version not supported
}

public sealed class WebdavStatusCode: HttpStatusCode
{
  private WebdavStatusCode() { }

  // http://tools.ietf.org/html/rfc4918#section-11

  public const int MultiStatus = 207;

  public const int UnprocessableEntity = 422;
  public const int Locked = 423;
  public const int FailedDependency = 424;

  public const int InsufficientStorage = 507;
}
About these ads

3 Responses to HTTP Status Codes in C#

  1. Jarrod Dixon says:

    There’s a System.Net.HttpStatusCode enum, if you’re interested.

    Too bad it’s not used by the System.Web.HttpResponseBase’s StatusCode property, or it would most likely be found!

  2. his point is that, .net only handles the basic http status codes….not from the webdav list, like 422, its a pain.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 69 other followers

%d bloggers like this: