HttpStatusCode Enumeration
Namespace: System.Net
Assembly: System.Net.Http.dll
Type: Enum
Inheritance: System.Object → System.ValueType → System.Enum → System.Net.HttpStatusCode
Declared in: System.Net.Http.dll
Description
Provides a set of values that are used to indicate the status of an HTTP request.
Members
The HttpStatusCode enumeration defines constants for the standard HTTP status codes. These codes are used to convey the result of an HTTP request from the server to the client.
Informational (1xx)
- Continue = 100
- SwitchingProtocols = 101
Success (2xx)
- OK = 200
- Created = 201
- Accepted = 202
- NonAuthoritativeInformation = 203
- NoContent = 204
- ResetContent = 205
- PartialContent = 206
Redirection (3xx)
- MultipleChoices = 300
- MovedPermanently = 301
- Found = 302
- SeeOther = 303
- NotModified = 304
- UseProxy = 305
- Unused = 306
- TemporaryRedirect = 307
- PermanentRedirect = 308
Client Error (4xx)
- BadRequest = 400
- Unauthorized = 401
- PaymentRequired = 402
- Forbidden = 403
- NotFound = 404
- MethodNotAllowed = 405
- NotAcceptable = 406
- ProxyAuthenticationRequired = 407
- RequestTimeout = 408
- Conflict = 409
- Gone = 410
- LengthRequired = 411
- PreconditionFailed = 412
- RequestEntityTooLarge = 413
- RequestUriTooLong = 414
- UnsupportedMediaType = 415
- RequestedRangeNotSatisfiable = 416
- ExpectationFailed = 417
- MisdirectedRequest = 421
- UnprocessableEntity = 422
- Locked = 423
- FailedDependency = 424
- UpgradeRequired = 426
- PreconditionRequired = 428
- TooManyRequests = 429
- RequestHeaderFieldsTooLarge = 431
Server Error (5xx)
- InternalServerError = 500
- NotImplemented = 501
- BadGateway = 502
- ServiceUnavailable = 503
- GatewayTimeout = 504
- HttpVersionNotSupported = 505
- VariantAlsoNegotiates = 506
- InsufficientStorage = 507
- LoopDetected = 508
- BandwidthLimitExceeded = 509
- NetworkAuthenticationRequired = 511
Example Usage
Here's how you might check the status code of an HTTP response:
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
public class HttpClientExample
{
public static async Task MakeRequestAsync()
{
using (var client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync("https://example.com");
// Check if the request was successful
if (response.StatusCode == HttpStatusCode.OK)
{
// Process the successful response
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine("Request successful!");
}
else
{
// Handle non-success status codes
Console.WriteLine($"Request failed with status code: {response.StatusCode}");
}
}
catch (HttpRequestException e)
{
Console.WriteLine($"An error occurred: {e.Message}");
}
}
}
}