HttpRequestHeaders Class
Represents the collection of HTTP headers that can be sent with an HTTP request.
Assembly: System.Net.Http.dll
Inheritance
object
HttpHeaders
HttpRequestHeaders
Summary
The HttpRequestHeaders class provides a strongly-typed container for HTTP headers that are sent as part of an HTTP request. It inherits from HttpHeaders and adds properties and methods specific to request headers, such as Host, UserAgent, and Accept.
Constructors
This class does not have public constructors. Instances are typically created internally by HttpRequestMessage.
Properties
| Name | Description |
|---|---|
Accept |
Gets or sets an ICollection<T> of MediaTypeWithQualityHeaderValue objects that can be sent with the request. These values indicate the media types which the client prefers. |
AcceptCharset |
Gets or sets an ICollection<T> of StringWithQualityHeaderValue objects that can be sent with the request. These values indicate the character sets that the client prefers. |
AcceptEncoding |
Gets or sets an ICollection<T> of StringWithQualityHeaderValue objects that can be sent with the request. These values indicate the content encodings that the client prefers. |
AcceptLanguage |
Gets or sets an ICollection<T> of StringWithQualityHeaderValue objects that can be sent with the request. These values indicate the natural languages that the client prefers. |
Authorization |
Gets or sets the value of the Authorization header. |
CacheControl |
Gets or sets a CacheControlHeaderValue object that represents the value of the Cache-Control header for the request. |
Connection |
Gets or sets a value that indicates whether the connection is to be closed after completion of the request. |
ConnectionClose |
Gets or sets a value that indicates whether the connection is to be closed after completion of the request. |
Cookie |
Gets or sets the value of the Cookie header. |
Date |
Gets or sets the value of the Date header. |
Expect |
Gets or sets the value of the Expect header. |
From |
Gets or sets the value of the From header. |
Host |
Gets or sets the value of the Host header. |
IfMatch |
Gets or sets an ICollection<T> of EntityTagHeaderValue objects that can be sent with the request. These values specify the required entity tags for the resource. |
IfModifiedSince |
Gets or sets a DateTimeOffset? value that represents the value of the If-Modified-Since header for the request. |
IfNoneMatch |
Gets or sets an ICollection<T> of EntityTagHeaderValue objects that can be sent with the request. These values specify the entity tags that are not desired for the resource. |
IfRange |
Gets or sets a RangeConditionHeaderValue object that represents the value of the If-Range header for the request. |
IfUnmodifiedSince |
Gets or sets a DateTimeOffset? value that represents the value of the If-Unmodified-Since header for the request. |
MaxForwards |
Gets or sets a value that specifies the maximum number of times that the request can be forwarded. |
Range |
Gets or sets a RangeHeaderValue object that represents the value of the Range header for the request. |
Referrer |
Gets or sets the value of the Referer header. |
Timeout |
Gets or sets the timeout for the request. |
TransferEncoding |
Gets or sets a collection of TransferCodingWithQualityHeaderValue objects that indicate the transfer encodings used for the request. |
TransferEncodingChunked |
Gets or sets a value that indicates whether the request is using chunked transfer encoding. |
Upgrade |
Gets or sets the value of the Upgrade header. |
UserAgent |
Gets or sets a ProductInfoHeaderValue object that represents the value of the User-Agent header for the request. |
Via |
Gets or sets a collection of ViaHeaderValue objects that specify the proxies that the request has passed through. |
Warning |
Gets or sets a collection of WarningHeaderValue objects that describe any warnings associated with the request. |
Methods
This class inherits methods from HttpHeaders and object, including methods for adding, getting, and removing headers.
Overridden Methods
| Name | Description |
|---|---|
Equals(object obj) |
Determines whether the specified object is equal to the current object. |
GetHashCode() |
Serves as the default hash function. |
GetType() |
Gets the type of the current instance. |
ToString() |
Returns a string that represents the current object. |
Remarks
The HttpRequestHeaders class is designed to simplify the management of HTTP request headers. It provides convenient properties for commonly used headers, allowing developers to set and retrieve these headers with ease. For custom headers not covered by specific properties, the base class HttpHeaders provides methods for general header manipulation.
When working with HttpClient, an instance of HttpRequestHeaders is accessible via the DefaultRequestHeaders property of the HttpClient object, allowing you to set headers that will be applied to all outgoing requests from that client instance.
Examples
using System.Net.Http;
using System.Net.Http.Headers;
// Create an HttpClient instance
using (HttpClient client = new HttpClient())
{
// Create a request message
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://api.example.com/data");
// Access and modify request headers
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.UserAgent.ParseAdd("MyAwesomeApp/1.0");
request.Headers.Add("X-Custom-Header", "SomeValue");
// Send the request (omitted for brevity)
// HttpResponseMessage response = await client.SendAsync(request);
}