HTTP support in .NET

Hypertext Transfer Protocol (or HTTP) is a protocol for requesting resources from a web server. The System.Net.Http.HttpClient class exposes the ability to send HTTP requests and receive HTTP responses from a resource identified by a URI. There are many types of resources that are available on the web, and HTTP defines a set of request methods for accessing these resources.

HTTP request methods

The request methods are differentiated via several factors, first by their verb but also by the following characteristics:

HTTP verb Is idempotent Is cacheable Is safe
GET ✔️ Yes ✔️ Yes ✔️ Yes
POST ❌ No ⚠️ Rarely ❌ No
PUT ✔️ Yes ❌ No ❌ No
PATCH ❌ No ❌ No ❌ No
DELETE ✔️ Yes ❌ No ❌ No
HEAD ✔️ Yes ✔️ Yes ✔️ Yes
OPTIONS ✔️ Yes ❌ No ✔️ Yes
TRACE ✔️ Yes ❌ No ✔️ Yes
CONNECT ❌ No ❌ No ❌ No

The POST method is only cacheable when the appropriate Cache-Control or Expires response headers are present. This is very uncommon in practice.

HTTP status codes

.NET provides comprehensive support for the HTTP protocol, which makes up the majority of all internet traffic, with the HttpClient. For more information, see Make HTTP requests with the HttpClient class. Applications receive HTTP protocol errors by catching an HttpRequestException with the HttpRequestException.StatusCode set to a corresponding HttpStatusCode. The HttpResponseMessage contains a HttpResponseMessage.StatusCode property that can be used to determine non-error status codes. For more information, see RFC 9110, HTTP Semantics: Status Codes.

Informational status codes

The informational status codes reflect an interim response. The client should continue to use the same request and discard the response.

HTTP status code HttpStatusCode
100 HttpStatusCode.Continue
101 HttpStatusCode.SwitchingProtocols
102 HttpStatusCode.Processing
103 HttpStatusCode.EarlyHints

Successful status codes

The successful status codes indicate that the client's request was successfully received, understood, and accepted.

HTTP status code HttpStatusCode
200 HttpStatusCode.OK
201 HttpStatusCode.Created
202 HttpStatusCode.Accepted
203 HttpStatusCode.NonAuthoritativeInformation
204 HttpStatusCode.NoContent
205 HttpStatusCode.ResetContent
206 HttpStatusCode.PartialContent
207 HttpStatusCode.MultiStatus
208 HttpStatusCode.AlreadyReported
226 HttpStatusCode.IMUsed

Redirection status codes

Redirection status codes require the user agent to take action in order to fulfill the request. With the appropriate headers, automatic redirection is possible.

HTTP status code HttpStatusCode
300 HttpStatusCode.MultipleChoices or HttpStatusCode.Ambiguous
301 HttpStatusCode.MovedPermanently or HttpStatusCode.Moved
302 HttpStatusCode.Found or HttpStatusCode.Redirect
303 HttpStatusCode.SeeOther or HttpStatusCode.RedirectMethod
304 HttpStatusCode.NotModified
305 HttpStatusCode.UseProxy
306 HttpStatusCode.Unused
307 HttpStatusCode.TemporaryRedirect or HttpStatusCode.RedirectKeepVerb
308 HttpStatusCode.PermanentRedirect

Client error status codes

The client error status codes indicate that the client's request was invalid.

HTTP status code HttpStatusCode
400 HttpStatusCode.BadRequest
401 HttpStatusCode.Unauthorized
402 HttpStatusCode.PaymentRequired
403 HttpStatusCode.Forbidden
404 HttpStatusCode.NotFound
405 HttpStatusCode.MethodNotAllowed
406 HttpStatusCode.NotAcceptable
407 HttpStatusCode.ProxyAuthenticationRequired
408 HttpStatusCode.RequestTimeout
409 HttpStatusCode.Conflict
410 HttpStatusCode.Gone
411 HttpStatusCode.LengthRequired
412 HttpStatusCode.PreconditionFailed
413 HttpStatusCode.RequestEntityTooLarge
414 HttpStatusCode.RequestUriTooLong
415 HttpStatusCode.UnsupportedMediaType
416 HttpStatusCode.RequestedRangeNotSatisfiable
417 HttpStatusCode.ExpectationFailed
418 I'm a teapot 🫖
421 HttpStatusCode.MisdirectedRequest
422 HttpStatusCode.UnprocessableEntity
423 HttpStatusCode.Locked
424 HttpStatusCode.FailedDependency
426 HttpStatusCode.UpgradeRequired
428 HttpStatusCode.PreconditionRequired
429 HttpStatusCode.TooManyRequests
431 HttpStatusCode.RequestHeaderFieldsTooLarge
451 HttpStatusCode.UnavailableForLegalReasons

Server error status codes

The server error status codes indicate that the server encountered an unexpected condition that prevented it from fulfilling the request.

HTTP status code HttpStatusCode
500 HttpStatusCode.InternalServerError
501 HttpStatusCode.NotImplemented
502 HttpStatusCode.BadGateway
503 HttpStatusCode.ServiceUnavailable
504 HttpStatusCode.GatewayTimeout
505 HttpStatusCode.HttpVersionNotSupported
506 HttpStatusCode.VariantAlsoNegotiates
507 HttpStatusCode.InsufficientStorage
508 HttpStatusCode.LoopDetected
510 HttpStatusCode.NotExtended
511 HttpStatusCode.NetworkAuthenticationRequired

See also