WebHeaderCollection.Remove 方法

定義

從集合中移除指定的標頭。Removes the specified header from the collection.

多載

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)

從集合中移除指定的標頭。Removes the specified header from the collection.

public:
 void Remove(System::Net::HttpRequestHeader header);
public void Remove (System.Net.HttpRequestHeader header);
override this.Remove : System.Net.HttpRequestHeader -> unit
Public Sub Remove (header As HttpRequestHeader)

參數

header
HttpRequestHeader

要從集合中移除的 HttpRequestHeader 執行個體。The HttpRequestHeader instance to remove from the collection.

例外狀況

這個 WebHeaderCollection 執行個體不允許 HttpRequestHeader 的執行個體。This WebHeaderCollection instance does not allow instances of HttpRequestHeader.

備註

Remove從集合中刪除指定的標頭。Remove deletes the specified header from the collection. 如果指定的標頭不存在,此方法不會執行任何操作。If the specified header does not exist, the method does nothing.

Remove(HttpResponseHeader)

從集合中移除指定的標頭。Removes the specified header from the collection.

public:
 void Remove(System::Net::HttpResponseHeader header);
public void Remove (System.Net.HttpResponseHeader header);
override this.Remove : System.Net.HttpResponseHeader -> unit
Public Sub Remove (header As HttpResponseHeader)

參數

header
HttpResponseHeader

要從集合中移除的 HttpResponseHeader 執行個體。The HttpResponseHeader instance to remove from the collection.

例外狀況

這個 WebHeaderCollection 執行個體不允許 HttpResponseHeader 的執行個體。This WebHeaderCollection instance does not allow instances of HttpResponseHeader.

備註

Remove從集合中刪除指定的標頭。Remove deletes the specified header from the collection. 如果指定的標頭不存在,此方法不會執行任何操作。If the specified header does not exist, the method does nothing.

Remove(String)

從集合中移除指定的標頭。Removes the specified header from the collection.

public:
 override void Remove(System::String ^ name);
public override void Remove (string name);
override this.Remove : string -> unit
Public Overrides Sub Remove (name As String)

參數

name
String

要從集合中移除的標頭名稱。The name of the header to remove from the collection.

例外狀況

namenullEmptyname is nullEmpty.

name 是受限制的標頭。name is a restricted header.

-或--or- name 包含無效的字元。name contains invalid characters.

範例

下列範例會使用Remove方法, WebHeaderCollection從移除標頭。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 S"www.msn.com".
   HttpWebRequest^ myHttpWebRequest = dynamic_cast<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 = dynamic_cast<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 = dynamic_cast<HttpWebResponse^>(myHttpWebRequest->GetResponse());

   // Print the headers of the new request with->Item[Out] 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 : {0}", e->Message );
}
catch ( WebException^ e ) 
{
   Console::WriteLine( "WebException is thrown. Message is : {0}", e->Message );
   if ( e->Status == WebExceptionStatus::ProtocolError )
   {
      Console::WriteLine( "Status Code : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->StatusCode );
      Console::WriteLine( "Status Description : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->StatusDescription );
      Console::WriteLine( "Server : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->Server );
   }
}
catch ( Exception^ e ) 
{
   Console::WriteLine( "Exception is thrown. Message is : {0}", e->Message );
}


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);
}
Public Shared Sub Main()
  Try
        'Create a web request for "www.msn.com".
        Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.msn.com"), HttpWebRequest)
        
        'Get the headers associated with the request.
        Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebRequest.Headers
        
        'Set the Cache-Control header.
        myWebHeaderCollection.Set("Cache-Control", "no-cache")
        
        'Get the associated response for the above request.
        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        
        '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")
        
        'Code example for "Remove" method of "WebHeaderCollection" ends here.
        'Get the response for the new request.
        myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        
        '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 e As ArgumentException
        Console.WriteLine("Error : Trying to remove a restricted header")
        Console.WriteLine(e.Message)
    Catch e As WebException
        Console.WriteLine(e.Message)
        If e.Status = WebExceptionStatus.ProtocolError Then
            Console.WriteLine("Status Code : {0}", CType(e.Response, HttpWebResponse).StatusCode)
            Console.WriteLine("Status Description : {0}", CType(e.Response, HttpWebResponse).StatusDescription)
            Console.WriteLine("Server : {0}", CType(e.Response, HttpWebResponse).Server)
        End If
    Catch e As Exception
        Console.WriteLine(e.Message)
    End Try

End Sub

備註

Remove從集合中刪除指定的標頭。Remove deletes the specified header from the collection. 如果指定的標頭不存在,則方法會傳回。If the specified header does not exist, the method returns.

適用於