July 31, 2009

HTTP Status Codes Class

One of my apps required to graph and chart IIS logs. I of course was using logparser to do this and everything seemed to be working out. The request changed on me slightly so that I can define what the status code is as opposed to just saying the number of 404's or 200's. Here is an easy Dictionary class that you can use:
public class HttpStatusCodes
{
static Dictionary _map = ConstructStatusCodesMap();

public static string GetDescription(int statusCode)
{
string description = "";
if (_map.ContainsKey(statusCode))
description = _map[statusCode];

return string.Format("{0} - {1}", statusCode, description);
}

private static Dictionary ConstructStatusCodesMap()
{
Dictionary map = new Dictionary();

// http://status-code.com/
#region Construct HTTP status codes map

map.Add(100, "Continue");
map.Add(101, "Switching Protocols");
map.Add(102, "Processing");
map.Add(200, "OK");
map.Add(201, "Created");
map.Add(202, "Accepted");
map.Add(203, "Non-Authoritative Information");
map.Add(204, "No Content");
map.Add(205, "Reset Content");
map.Add(206, "Partial Content");
map.Add(207, "Multi-Status");
map.Add(226, "IM Used");
map.Add(300, "Multiple Choices");
map.Add(301, "Moved Permanently");
map.Add(302, "Found");
map.Add(303, "See Other");
map.Add(304, "Not Modified");
map.Add(305, "Use Proxy");
map.Add(306, "(Unused)");
map.Add(307, "Temporary Redirect");
map.Add(400, "Bad Request");
map.Add(401, "Unauthorized");
map.Add(402, "Payment Required");
map.Add(403, "Forbidden");
map.Add(404, "Not Found");
map.Add(405, "Method Not Allowed");
map.Add(406, "Not Acceptable");
map.Add(407, "Proxy Authentication Required");
map.Add(408, "Request Timeout");
map.Add(409, "Conflict");
map.Add(410, "Gone");
map.Add(411, "Length Required");
map.Add(412, "Precondition Failed");
map.Add(413, "Request Entity Too Large");
map.Add(414, "Request-URI Too Long");
map.Add(415, "Unsupported Media Type");
map.Add(416, "Requested Range Not Satisfiable");
map.Add(417, "Expectation Failed");
map.Add(418, "I'm a teapot");
map.Add(422, "Unprocessable Entity");
map.Add(423, "Locked");
map.Add(424, "Failed Dependency");
map.Add(425, "(Unordered Collection)");
map.Add(426, "Upgrade Required");
map.Add(500, "Internal Server Error");
map.Add(501, "Not Implemented");
map.Add(502, "Bad Gateway");
map.Add(503, "Service Unavailable");
map.Add(504, "Gateway Timeout");
map.Add(505, "HTTP Version Not Supported");
map.Add(506, "Variant Also Negotiates");
map.Add(507, "Insufficient Storage");
map.Add(510, "Not Extended");

#endregion

return map;
}
}
Using it would be something like:
 return HttpStatusCodes.GetDescription((int)value);

No comments:

Post a Comment