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. Many types of resources 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:
- A request method is idempotent if it can be successfully processed multiple times without changing the result. For more information, see RFC 9110: 9.2.2. Idempotent Methods.
- A request method is cacheable when its corresponding response can be stored for reuse. For more information, see RFC 9110: Section 9.2.3. Methods and Caching.
- A request method is considered a safe method if it doesn't modify the state of a resource. All safe methods are also idempotent, but not all idempotent methods are considered safe. For more information, see RFC 9110: Section 9.2.1. Safe Methods.
HTTP method | 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 appropriateCache-Control
orExpires
response headers are present. This is very uncommon in practice.
HTTP status codes
.NET provides comprehensive support for the HTTP protocol, which accounts for most 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. HTTP status codes are either reported in HttpResponseMessage with the HttpResponseMessage.StatusCode or in HttpRequestException with the HttpRequestException.StatusCode in case the called method doesn't return a response message. For more information about error handling, see HTTP error handling, and for more information about status codes, see RFC 9110, HTTP Semantics: Status Codes.
Informational status codes
The informational status codes reflect an interim response. Most of the interim responses, for example HttpStatusCode.Continue, are handled internally with HttpClient and are never surfaced to the user.
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 to fulfill the request. Automatic redirection is turned on by default, it can be changed with HttpClientHandler.AllowAutoRedirect or SocketsHttpHandler.AllowAutoRedirect.
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.
Server error status codes
The server error status codes indicate that the server encountered an unexpected condition that prevented it from fulfilling the request.