WebHeaderCollection.Add Method

Definition

Inserts a new header into the collection.

Overloads

Add(String)

Inserts the specified header into the collection.

Add(HttpRequestHeader, String)

Inserts the specified header with the specified value into the collection.

Add(HttpResponseHeader, String)

Inserts the specified header with the specified value into the collection.

Add(String, String)

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

Add(String)

Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs

Inserts the specified header into the collection.

public void Add (string header);

Parameters

header
String

The header to add, with the name and value separated by a colon.

Exceptions

header is null or Empty.

header does not contain a colon (:) character.

The length of value is greater than 65535.

-or-

The name part of header is Empty or contains invalid characters.

-or-

header is a restricted header that should be set with a property.

-or-

The value part of header contains invalid characters.

.NET Framework and .NET Core versions 2.0 - 3.1 only: The length of the string after the colon (:) is greater than 65535.

Examples

The following example adds a name/value pair to a WebHeaderCollection using the Add Method.

try {
    //Create a web request for "www.msn.com".
    HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("http://www.msn.com");

    //Get the headers associated with the request.
    WebHeaderCollection myWebHeaderCollection = myHttpWebRequest.Headers;

    Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method");

    //Add the Accept-Language header (for Danish) in the request.
    myWebHeaderCollection.Add("Accept-Language:da");

    //Include English in the Accept-Langauge header. 
    myWebHeaderCollection.Add("Accept-Language","en;q=0.8");

    //Get the associated response for the above request.
    HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();

    //Print the headers for the request.
    printHeaders(myWebHeaderCollection);
    myHttpWebResponse.Close();
}
//Catch exception if trying to add a restricted header.
catch(ArgumentException e) {
    Console.WriteLine(e.Message);
}
catch(WebException e) {
    Console.WriteLine("\nWebException is thrown. \nMessage is :" + e.Message);
    if(e.Status == WebExceptionStatus.ProtocolError) {
        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
        Console.WriteLine("Server : {0}", ((HttpWebResponse)e.Response).Server);
    }
}
catch(Exception e) {
    Console.WriteLine("Exception is thrown. Message is :" + e.Message);
}

Nota

The length of the value portion of header, that is, the string after the colon (:), is validated only in .NET Framework and .NET Core versions 2.0 - 3.1.

Remarks

The header parameter must be specified in the format "name:value". If the specified header does not exist in the collection, a new header is added to the collection.

If the header specified in header is already present in the collection, the value part of the header is concatenated with the existing value.

Applies to

.NET 9 dan versi lain
Produk Versi
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(HttpRequestHeader, String)

Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs

Inserts the specified header with the specified value into the collection.

public void Add (System.Net.HttpRequestHeader header, string? value);
public void Add (System.Net.HttpRequestHeader header, string value);

Parameters

header
HttpRequestHeader

The header to add to the collection.

value
String

The content of the header.

Exceptions

.NET Framework and .NET Core versions 2.0 - 3.1 only: The length of value is greater than 65535.

This WebHeaderCollection instance does not allow instances of HttpRequestHeader.

Remarks

If the specified header does not exist, the Add method inserts a new header into the list of header name/value pairs.

If the specified header is already present, value is added to the comma-separated list of values associated with the header.

Nota

The length of value is validated only in .NET Framework and .NET Core versions 2.0 - 3.1.

Applies to

.NET 9 dan versi lain
Produk Versi
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(HttpResponseHeader, String)

Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs

Inserts the specified header with the specified value into the collection.

public void Add (System.Net.HttpResponseHeader header, string? value);
public void Add (System.Net.HttpResponseHeader header, string value);

Parameters

header
HttpResponseHeader

The header to add to the collection.

value
String

The content of the header.

Exceptions

.NET Framework and .NET Core versions 2.0 - 3.1 only: The length of value is greater than 65535.

This WebHeaderCollection instance does not allow instances of HttpResponseHeader.

Remarks

If the specified header does not exist, the Add method inserts a new header into the list of header name/value pairs.

If the specified header is already present, value is added to the comma-separated list of values associated with the header.

Nota

The length of value is validated only in .NET Framework and .NET Core versions 2.0 - 3.1.

Applies to

.NET 9 dan versi lain
Produk Versi
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(String, String)

Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs

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

public override void Add (string name, string? value);
public override void Add (string name, string value);

Parameters

name
String

The header to add to the collection.

value
String

The content of the header.

Exceptions

name is null, Empty, or contains invalid characters.

-or-

name is a restricted header that must be set with a property setting.

-or-

value contains invalid characters.

.NET Framework and .NET Core versions 2.0 - 3.1 only: The length of value is greater than 65535.

Examples

The following example adds a name/value pair to a WebHeaderCollection using the Add Method.

try {
    //Create a web request for "www.msn.com".
    HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("http://www.msn.com");

    //Get the headers associated with the request.
    WebHeaderCollection myWebHeaderCollection = myHttpWebRequest.Headers;

    Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method");

    //Add the Accept-Language header (for Danish) in the request.
    myWebHeaderCollection.Add("Accept-Language:da");

    //Include English in the Accept-Langauge header. 
    myWebHeaderCollection.Add("Accept-Language","en;q=0.8");

    //Get the associated response for the above request.
    HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();

    //Print the headers for the request.
    printHeaders(myWebHeaderCollection);
    myHttpWebResponse.Close();
}
//Catch exception if trying to add a restricted header.
catch(ArgumentException e) {
    Console.WriteLine(e.Message);
}
catch(WebException e) {
    Console.WriteLine("\nWebException is thrown. \nMessage is :" + e.Message);
    if(e.Status == WebExceptionStatus.ProtocolError) {
        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
        Console.WriteLine("Server : {0}", ((HttpWebResponse)e.Response).Server);
    }
}
catch(Exception e) {
    Console.WriteLine("Exception is thrown. Message is :" + e.Message);
}

Nota

The length of value is validated only in .NET Framework and .NET Core versions 2.0 - 3.1.

Remarks

If the header specified in name does not exist, the Add method inserts a new header into the list of header name/value pairs.

If the header specified in name is already present, value is added to the existing comma-separated list of values associated with name.

Applies to

.NET 9 dan versi lain
Produk Versi
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1