HttpResponseHeaders Class
Represents the collection of HTTP headers that are part of an HTTP response.
System.Net.Http.dll
Overview
The HttpResponseHeaders class provides a strongly-typed way to access and manipulate the headers sent with an HTTP response. It inherits from HttpHeaders, which offers a base implementation for managing header key-value pairs.
This class is typically accessed through the Headers property of an HttpRequestMessage or HttpResponseMessage object.
Syntax
public abstract class HttpResponseHeaders : System.Net.Http.HttpHeaders
Remarks
The HttpResponseHeaders class allows you to work with standard HTTP response headers such as Cache-Control, Content-Encoding, Date, Server, and Transfer-Encoding. It also supports custom headers.
When you retrieve headers from an HttpResponseMessage, you'll often get an instance of HttpResponseHeaders. This class provides convenient methods for adding, removing, and querying specific header values.
Members
Properties
-
Cache-Control
Gets or sets the value of the HTTP
Cache-Controlheader. -
Connection
Gets or sets the value of the HTTP
Connectionheader. -
Content-Encoding
Gets or sets the value of the HTTP
Content-Encodingheader. -
Content-Language
Gets or sets the value of the HTTP
Content-Languageheader. -
Content-Length
Gets or sets the value of the HTTP
Content-Lengthheader. -
Content-Type
Gets or sets the value of the HTTP
Content-Typeheader. -
Date
Gets or sets the value of the HTTP
Dateheader. -
Expires
Gets or sets the value of the HTTP
Expiresheader. -
Last-Modified
Gets or sets the value of the HTTP
Last-Modifiedheader. -
Server
Gets or sets the value of the HTTP
Serverheader. -
Transfer-Encoding
Gets or sets the value of the HTTP
Transfer-Encodingheader. -
Vary
Gets or sets the value of the HTTP
Varyheader.
Methods
- Add(string name, string value) Adds a header with the specified name and value.
- Contains(string name) Determines whether the collection contains a header with the specified name.
- GetValues(string name) Gets the values of a header with the specified name.
- Remove(string name) Removes a header with the specified name from the collection.
- TryAddWithoutValidation(string name, string value) Attempts to add a header with the specified name and value without performing validation.
- TryGetValues(string name, out IEnumerable<string> values) Attempts to get the values of a header with the specified name.
Example
Here's a simple example of how to access and modify response headers:
using System.Net.Http;
// Assuming 'response' is an HttpResponseMessage
HttpResponseMessage response = new HttpResponseMessage();
// Accessing the headers collection
HttpResponseHeaders headers = response.Headers;
// Adding a custom header
headers.Add("X-Custom-Header", "MyValue");
// Setting a standard header
headers.Server = new System.Net.Http.Headers.ProductInfoHeaderValue("MyServerApp");
// Getting a header value
if (headers.Contains("X-Custom-Header"))
{
string customHeaderValue = headers.GetValues("X-Custom-Header").FirstOrDefault();
// Use customHeaderValue
}
// Removing a header
headers.Remove("X-Custom-Header");