System.Net.WebHeaderCollection

Represents a collection of HTTP headers. This class cannot be inherited.

Table of Contents

Properties

Count
Gets the number of elements contained in the WebHeaderCollection instance.
public int Count { get; }
Keys
Gets a WebHeaderCollection.HeaderStringCollection object that contains all the header names in the WebHeaderCollection instance.
public System.Collections.Specialized.NameObjectCollectionBase.KeysCollection Keys { get; }

Methods

Add(string name, string value)

Adds a header with the specified name and value to the collection. If the header already exists, its value is appended.

public void Add(string name, string value);

Parameters

Name Type Description
name string The name of the header to add. Header names are case-insensitive.
value string The value of the header to add.

Get(string name)

Gets the value of the specified header from the collection.

public string Get(string name);

Parameters

Name Type Description
name string The name of the header to retrieve. Header names are case-insensitive.

Returns

The string value of the specified header, or null if the header is not found.

GetValue(int index)

Gets the value of the header at the specified index.

public string GetValue(int index);

Parameters

Name Type Description
index int The zero-based index of the header to retrieve.

Returns

The string value of the header at the specified index.

GetValues(string name)

Gets all the values associated with the specified header name from the collection.

public string[] GetValues(string name);

Parameters

Name Type Description
name string The name of the header to retrieve. Header names are case-insensitive.

Returns

An array of strings containing all the values associated with the header, or null if the header is not found.

Remove(string name)

Removes the specified header from the collection.

public void Remove(string name);

Parameters

Name Type Description
name string The name of the header to remove. Header names are case-insensitive.

Set(string name, string value)

Adds or replaces a header with the specified name and value in the collection. If the header already exists, its value is overwritten.

public void Set(string name, string value);

Parameters

Name Type Description
name string The name of the header to set. Header names are case-insensitive.
value string The value of the header to set.

Examples

Adding Headers

using System.Net;

WebHeaderCollection headers = new WebHeaderCollection();
headers.Add("User-Agent", "MyAwesomeApp/1.0");
headers.Add("Accept", "application/json");
headers.Add("X-Custom-Header", "SomeValue");

Console.WriteLine($"User-Agent: {headers["User-Agent"]}");
// Output: User-Agent: MyAwesomeApp/1.0

Console.WriteLine($"Count: {headers.Count}");
// Output: Count: 3

Retrieving and Modifying Headers

using System.Net;

WebHeaderCollection headers = new WebHeaderCollection();
headers.Add("Content-Type", "text/plain");
headers.Add("Content-Type", "charset=utf-8"); // Adds a second value

string contentType = headers["Content-Type"];
Console.WriteLine($"First Content-Type: {contentType}");
// Output: First Content-Type: text/plain

string[] allContentTypes = headers.GetValues("Content-Type");
Console.WriteLine($"All Content-Types: {string.Join(", ", allContentTypes)}");
// Output: All Content-Types: text/plain, charset=utf-8

headers.Set("Content-Type", "application/json"); // Overwrites existing values
Console.WriteLine($"Updated Content-Type: {headers["Content-Type"]}");
// Output: Updated Content-Type: application/json

headers.Remove("Accept"); // If Accept header was added