Library

HttpStatusCode Enum

System.Net.Http

Defines standard HTTP status codes.

Summary

The HttpStatusCode enumeration provides a convenient way to represent the various HTTP status codes returned by a web server. These codes are crucial for understanding the result of an HTTP request, indicating whether it was successful, encountered an error, or required further action.

Members

Member Value Description
Continue 100 100 Continue: The request has been received by the server, and the client should continue with the request.
SwitchingProtocols 101 101 Switching Protocols: The server has switched protocols in accordance with a request header.
OK 200 200 OK: The request has succeeded.
Created 201 201 Created: The request has been fulfilled and resulted in a new resource being created.
Accepted 202 202 Accepted: The request has been accepted for processing, but the processing has not been completed.
NonAuthoritativeInformation 203 203 Non-Authoritative Information: The returned metainformation is the default version that has been modified by a local or proxy copy of the resource, and is thus not the 'authoritative' version.
NoContent 204 204 No Content: There is no content to send back to the client for this request.
ResetContent 205 205 Reset Content: The server has fulfilled the request and the client should reset the document view.
PartialContent 206 206 Partial Content: The server is returning the requested range of the resource.
MultipleChoices 300 300 Multiple Choices: The request has more than one possible response.
MovedPermanently 301 301 Moved Permanently: The requested resource has been assigned a new permanent URI and any future references to this resource should use that new URI.
Found 302 302 Found: The requested resource resides temporarily under a different URI.
SeeOther 303 303 See Other: The response to the request can be found under a different URI, and the client should use the GET method to retrieve it.
NotModified 304 304 Not Modified: The client has performed a conditional GET request to a resource that has not been modified since the condition evaluated to true.
UseProxy 305 305 Use Proxy: The requested resource is available only through a proxy.
Unused 306 306 Unused: This is a reserved status code.
TemporaryRedirect 307 307 Temporary Redirect: The requested resource resides temporarily under a different URI.
PermanentRedirect 308 308 Permanent Redirect: The requested resource resides permanently under a different URI.
BadRequest 400 400 Bad Request: The request could not be understood by the server due to malformed syntax.
Unauthorized 401 401 Unauthorized: The requested resource requires authentication.
PaymentRequired 402 402 Payment Required: Reserved for future use.
Forbidden 403 403 Forbidden: The server understood the request, but refuses to authorize it.
NotFound 404 404 Not Found: The requested resource could not be found.
MethodNotAllowed 405 405 Method Not Allowed: The request method is not supported for the requested resource.
NotAcceptable 406 406 Not Acceptable: The client will not accept the response content.
ProxyAuthenticationRequired 407 407 Proxy Authentication Required: The client must authenticate itself with the proxy.
RequestTimeout 408 408 Request Timeout: The server timed out waiting for the request.
Conflict 409 409 Conflict: The request could not be completed because of a conflict.
Gone 410 410 Gone: The requested resource is no longer available.
LengthRequired 411 411 Length Required: The Content-length header is missing.
PreconditionFailed 412 412 Precondition Failed: The precondition given in one or more of the request header fields evaluated to false.
RequestEntityTooLarge 413 413 Request Entity Too Large: The client attempted to upload a message that is too large.
RequestUriTooLong 414 414 Request URI Too Long: The client attempted to request a URI that is too long.
UnsupportedMediaType 415 415 Unsupported Media Type: The client attempted to upload a file of a type not allowed.
RequestedRangeNotSatisfiable 416 416 Requested Range Not Satisfiable: The client requested a range of the resource that is not available from the server.
ExpectationFailed 417 417 Expectation Failed: The server could not meet the requirements of the Expect header field.
UpgradeRequired 426 426 Upgrade Required: The server refuses to perform the request without a prior upgrade to a specified protocol.
InternalServerError 500 500 Internal Server Error: The server encountered an unexpected condition that prevented it from fulfilling the request.
NotImplemented 501 501 Not Implemented: The server does not support the functionality required to fulfill the request.
BadGateway 502 502 Bad Gateway: The server, while acting as a gateway or proxy, received an invalid response from the upstream server.
ServiceUnavailable 503 503 Service Unavailable: The server is temporarily unable to service the request due to an overload or maintenance.
GatewayTimeout 504 504 Gateway Timeout: The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server.
HttpVersionNotSupported 505 505 HTTP Version Not Supported: The server does not support the HTTP protocol version that was used in the request.

Usage Example

You can use the HttpStatusCode enumeration to check the status of an HTTP response:

using System.Net.Http;

public class HttpHelper
{
    public static bool IsSuccess(HttpResponseMessage response)
    {
        return response.StatusCode == HttpStatusCode.OK ||
               response.StatusCode == HttpStatusCode.Created ||
               response.StatusCode == HttpStatusCode.Accepted;
    }

    public static void ProcessResponse(HttpResponseMessage response)
    {
        if (response.StatusCode == HttpStatusCode.NotFound)
        {
            Console.WriteLine("Resource not found.");
        }
        else if (response.StatusCode >= HttpStatusCode.BadRequest && response.StatusCode < HttpStatusCode.InternalServerError)
        {
            Console.WriteLine($"Client error: {(int)response.StatusCode}");
        }
        else if (response.StatusCode >= HttpStatusCode.InternalServerError)
        {
            Console.WriteLine($"Server error: {(int)response.StatusCode}");
        }
        else
        {
            Console.WriteLine($"Request successful with status code: {(int)response.StatusCode}");
        }
    }
}

Remarks

The HTTP status codes are grouped into five classes:

The HttpStatusCode enum provides a strongly-typed representation of these codes, making your code more readable and maintainable compared to using raw integer values.