HttpResponseHeaders Class

Represents the collection of HTTP headers that are part of an HTTP response.

Namespace: System.Net.Http
Assembly: 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-Control header.
  • Connection Gets or sets the value of the HTTP Connection header.
  • Content-Encoding Gets or sets the value of the HTTP Content-Encoding header.
  • Content-Language Gets or sets the value of the HTTP Content-Language header.
  • Content-Length Gets or sets the value of the HTTP Content-Length header.
  • Content-Type Gets or sets the value of the HTTP Content-Type header.
  • Date Gets or sets the value of the HTTP Date header.
  • Expires Gets or sets the value of the HTTP Expires header.
  • Last-Modified Gets or sets the value of the HTTP Last-Modified header.
  • Server Gets or sets the value of the HTTP Server header.
  • Transfer-Encoding Gets or sets the value of the HTTP Transfer-Encoding header.
  • Vary Gets or sets the value of the HTTP Vary header.

Methods

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");