Table of Contents

Class WebHeaderCollection

Namespace
System.Net
Assembly
System.Net.Http.dll

Contains protocol headers associated with a request or response. Manages name-value pairs for HTTP headers.

public class WebHeaderCollection
Inheritance
WebHeaderCollection
Inherited Members
Extension Methods

Remarks

This class includes additional methods, including HTTP parsing of a collection into a buffer that can be sent.

Headers are validated when attempting to add them.

Constructors

WebHeaderCollection()

Creates an empty collection of WEB headers.

public WebHeaderCollection()

Properties

AllKeys

Gets all header names (keys) in the collection.

public string[] AllKeys { get; }

Property Value

string[]

An array of type String containing all header names in a Web request.

Count

Gets the number of headers in the collection.

public int Count { get; }

Property Value

int

An Int32 indicating the number of headers in a request.

this[string]

Returns the string value for the header.

public string this[string header] { get; }

Parameters

header string

The name of the header.

Property Value

string

A string containing the value. If no value is present, returns null.

Methods

Add(string)

Inserts a new header into the collection.

public void Add(string header)

Parameters

header string

A header name/value pair, in the format "myHeaderName:myValue".

Remarks

This method expects a string with the format "myName:myValue", and parses the two parts out.

If a header with the specified name already exists, the header that is being added is concatenated onto the existing header.

Throws an exception if the specified header name is the name of a special header.

Add(string, string)

Inserts a header with the specified name and value into the collection.

public void Add(string name, string value)

Parameters

name string

The name of the header that is being added to the collection.

value string

The content of the header that is being added (its header-value). If a header with the specified name already exists, this value is concatenated onto the existing header.

Remarks

If a header with the specified name already exists, the header that is being added is concatenated onto the existing header.

Throws an exception if the specified header name is the name of a special header.

GetValues(string)

Returns the values for the specified header name.

public string[] GetValues(string header)

Parameters

header string

The name of the header.

Returns

string[]

An array of parsed string objects.

Remarks

Takes a header name and returns a string array representing the individual values for that header. For example, if the headers contain the following line:

Accept: text/plain, text/html

then GetValues("Accept") returns an array of two strings: "text/plain" and "text/html".

IsRestricted(string)

Tests whether the specified HTTP header can be set.

public static bool IsRestricted(string headerName)

Parameters

headerName string

Name for the header.

Returns

bool

Remarks

Throws an exception if the header name is blank, contains illegal characters, or contains characters that are reserved by the HTTP protocol.

Remove(string)

Removes the specified header from the collection.

public void Remove(string name)

Parameters

name string

The name of the header to remove.

Remarks

Throws an exception if the specified header name is the name of a special header.

Set(string, string)

Sets the specified header to the specified value.

public void Set(string name, string value)

Parameters

name string

The header to set.

value string

The content of the header to set.

Remarks

Includes validation. Throws an exception if the specified header name is the name of a special header.

ToByteArray()

Generates a byte array representation of the headers, that is ready to be sent.

public byte[] ToByteArray()

Returns

byte[]

An array of bytes.

Remarks

This method serializes the headers into a byte array that can be sent over the network. The format looks like:

Header-Name1: Header-Value1\r\n
Header-Name2: Header-Value2\r\n
...
Header-NameN: Header-ValueN\r\n
\r\n

ToString()

Generates a string representation of the headers, that is ready to be sent except for it being in String format.

public override string ToString()

Returns

string

A string representation of the headers.

Remarks

The format looks like the following:

Header-Name: Header-Value\r\n
Header-Name2: Header-Value2\r\n
...
Header-NameN: Header-ValueN\r\n
\r\n