WebHeaderCollection.Set Method

Definition

Sets the specified header to the specified value.

Overloads

Set(HttpRequestHeader, String)

Sets the specified header to the specified value.

Set(HttpResponseHeader, String)

Sets the specified header to the specified value.

Set(String, String)

Sets the specified header to the specified value.

Set(HttpRequestHeader, String)

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

Sets the specified header to the specified value.

C#
public void Set(System.Net.HttpRequestHeader header, string? value);
C#
public void Set(System.Net.HttpRequestHeader header, string value);

Parameters

header
HttpRequestHeader

The HttpRequestHeader value to set.

value
String

The content of the header to set.

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 header specified in the header does not exist, the Set method inserts a new header into the list of header name/value pairs.

If the header specified in header is already present, value replaces the existing value.

Note

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

Applies to

.NET 9 and other versions
Product Versions
.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

Set(HttpResponseHeader, String)

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

Sets the specified header to the specified value.

C#
public void Set(System.Net.HttpResponseHeader header, string? value);
C#
public void Set(System.Net.HttpResponseHeader header, string value);

Parameters

header
HttpResponseHeader

The HttpResponseHeader value to set.

value
String

The content of the header to set.

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 header specified in the header does not exist, the Set method inserts a new header into the list of header name/value pairs.

If the header specified in header is already present, value replaces the existing value.

Note

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

Applies to

.NET 9 and other versions
Product Versions
.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

Set(String, String)

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

Sets the specified header to the specified value.

C#
public override void Set(string name, string? value);
C#
public override void Set(string name, string value);

Parameters

name
String

The header to set.

value
String

The content of the header to set.

Exceptions

name is null or Empty.

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

name is a restricted header.

-or-

name or value contain invalid characters.

Examples

The following example uses the Set method to set the value of an existing header.

C#
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;

    // Set the Cache-Control header in the request.
    myWebHeaderCollection.Set("Cache-Control", "no-cache");

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

    Console.WriteLine ("Headers after 'Set' method is used on Cache-Control :");
    // Print the headers for the request.
    PrintHeaders(myWebHeaderCollection);
    myHttpWebResponse.Close();
}
// Catch exception if trying to set a restricted header.
catch(ArgumentException e) {
    Console.WriteLine("ArgumentException is thrown. Message is :" + e.Message);
}
catch(WebException e) {
    Console.WriteLine("WebException is thrown. Message 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);
}

Note

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

Remarks

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

If the header specified in header is already present, value replaces the existing value.

Applies to

.NET 9 and other versions
Product Versions
.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