Share via


HTTP-stöd i .NET

Hypertext Transfer Protocol (eller HTTP) är ett protokoll för att begära resurser från en webbserver. Klassen System.Net.Http.HttpClient visar möjligheten att skicka HTTP-begäranden och ta emot HTTP-svar från en resurs som identifieras av en URI. Många typer av resurser är tillgängliga på webben och HTTP definierar en uppsättning metoder för att begära åtkomst till dessa resurser.

HTTP-begärandemetoder

Begärandemetoderna särskiljs via flera faktorer, först av deras verb men också av följande egenskaper:

HTTP-metod Är idempotent Kan cachelagrats Är säkert
GET ✔️ Ja ✔️ Ja ✔️ Ja
POST ❌ Nej ⚠️ Rarely ❌ Nej
PUT ✔️ Ja ❌ Nej ❌ Nej
PATCH ❌ Nej ❌ Nej ❌ Nej
DELETE ✔️ Ja ❌ Nej ❌ Nej
HEAD ✔️ Ja ✔️ Ja ✔️ Ja
OPTIONS ✔️ Ja ❌ Nej ✔️ Ja
TRACE ✔️ Ja ❌ Nej ✔️ Ja
CONNECT ❌ Nej ❌ Nej ❌ Nej

POST†Metoden kan bara cachelagrats när lämpliga Cache-Control sidhuvuden eller Expires svarshuvuden finns. Detta är mycket ovanligt i praktiken.

HTTP-statuskoder

.NET ger omfattande stöd för HTTP-protokollet, som står för den största delen av Internettrafiken, med HttpClient. Mer information finns i Skapa HTTP-begäranden med klassen HttpClient. Program får HTTP-protokollfel genom att fånga en HttpRequestException. HTTP-statuskoder rapporteras antingen i HttpResponseMessage med HttpResponseMessage.StatusCode eller i HttpRequestException om HttpRequestException.StatusCode den anropade metoden inte returnerar ett svarsmeddelande. Mer information om felhantering finns i HTTP-felhantering och mer information om statuskoder finns i RFC 9110, HTTP-semantik: statuskoder.

Informationsstatuskoder

Informationsstatuskoderna återspeglar ett interimsvar. De flesta interimsvar, till exempel HttpStatusCode.Continue, hanteras internt med HttpClient och visas aldrig för användaren.

HTTP-statuskod HttpStatusCode
100 HttpStatusCode.Continue
101 HttpStatusCode.SwitchingProtocols
102 HttpStatusCode.Processing
103 HttpStatusCode.EarlyHints

Lyckade statuskoder

De lyckade statuskoderna anger att klientens begäran har tagits emot, förståtts och godkänts.

HTTP-statuskod 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

Statuskoder för omdirigering

Omdirigeringsstatuskoder kräver att användaragenten vidtar åtgärder för att uppfylla begäran. Automatisk omdirigering är aktiverat som standard, det kan ändras med HttpClientHandler.AllowAutoRedirect eller SocketsHttpHandler.AllowAutoRedirect.

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

Statuskoder för klientfel

Statuskoderna för klientfel anger att klientens begäran var ogiltig.

HTTP-statuskod 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 Jag är en tekanna 🫖
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

Statuskoder för serverfel

Statuskoderna för serverfel anger att servern påträffade ett oväntat villkor som hindrade den från att uppfylla begäran.

HTTP-statuskod 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

Se även