WebHeaderCollection.Remove Method

Definition

Removes the specified header from the collection.

Overloads

Remove(HttpRequestHeader)

Removes the specified header from the collection.

Remove(HttpResponseHeader)

Removes the specified header from the collection.

Remove(String)

Removes the specified header from the collection.

Remove(HttpRequestHeader)

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

Removes the specified header from the collection.

public void Remove (System.Net.HttpRequestHeader header);

Parameters

header
HttpRequestHeader

The HttpRequestHeader instance to remove from the collection.

Exceptions

This WebHeaderCollection instance does not allow instances of HttpRequestHeader.

Remarks

Remove deletes the specified header from the collection. If the specified header does not exist, the method does nothing.

Applies to

.NET 9 og andre versioner
Produkt 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

Remove(HttpResponseHeader)

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

Removes the specified header from the collection.

public void Remove (System.Net.HttpResponseHeader header);

Parameters

header
HttpResponseHeader

The HttpResponseHeader instance to remove from the collection.

Exceptions

This WebHeaderCollection instance does not allow instances of HttpResponseHeader.

Remarks

Remove deletes the specified header from the collection. If the specified header does not exist, the method does nothing.

Applies to

.NET 9 og andre versioner
Produkt 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

Remove(String)

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

Removes the specified header from the collection.

public void Remove (string name);
public override void Remove (string name);

Parameters

name
String

The name of the header to remove from the collection.

Exceptions

name is a restricted header.

-or-

name contains invalid characters.

Examples

The following example uses the Remove method to remove a header from the WebHeaderCollection. After the header is removed, this example prints all existing headers to the screen to prove that it has been removed.

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.
    myWebHeaderCollection.Set("Cache-Control", "no-cache");

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

    // Print the headers of the request to console.
    Console.WriteLine("Print request headers after adding Cache-Control for first request:");
    printHeaders(myHttpWebRequest.Headers);

    // Remove the Cache-Control header for the new request.
    myWebHeaderCollection.Remove("Cache-Control");

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

    // Print the headers of the new request without the Cache-Control header.
    Console.WriteLine("Print request headers after removing Cache-Control for the new request:");
    printHeaders(myHttpWebRequest.Headers);
    myHttpWebResponse.Close();
}
// Catch exception if trying to remove a restricted header.
catch(ArgumentException e) {
    Console.WriteLine("Error : Trying to remove a restricted header");
    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);
}

Remarks

Remove deletes the specified header from the collection. If the specified header does not exist, the method returns.

Applies to

.NET 9 og andre versioner
Produkt Versions
.NET Core 1.0, Core 1.1, 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
UWP 10.0