WebHeaderCollection.Set 方法

定義

將指定的標頭設定為指定的值。Sets the specified header to the specified value.

多載

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)

將指定的標頭設定為指定的值。Sets the specified header to the specified value.

public:
 void Set(System::Net::HttpRequestHeader header, System::String ^ value);
public void Set (System.Net.HttpRequestHeader header, string value);
override this.Set : System.Net.HttpRequestHeader * string -> unit
Public Sub Set (header As HttpRequestHeader, value As String)

參數

header
HttpRequestHeader

要設定的 HttpRequestHeader 值。The HttpRequestHeader value to set.

value
String

要設定的標頭內容。The content of the header to set.

例外狀況

value 的長度大於 65535。The length of value is greater than 65535.

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

備註

如果標頭中指定的標頭不存在,則Set方法會將新的標頭插入標頭名稱/值組的清單中。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.

如果中header指定的標頭已存在, value則會取代現有的值。If the header specified in header is already present, value replaces the existing value.

Set(HttpResponseHeader, String)

將指定的標頭設定為指定的值。Sets the specified header to the specified value.

public:
 void Set(System::Net::HttpResponseHeader header, System::String ^ value);
public void Set (System.Net.HttpResponseHeader header, string value);
override this.Set : System.Net.HttpResponseHeader * string -> unit
Public Sub Set (header As HttpResponseHeader, value As String)

參數

header
HttpResponseHeader

要設定的 HttpResponseHeader 值。The HttpResponseHeader value to set.

value
String

要設定的標頭內容。The content of the header to set.

例外狀況

value 的長度大於 65535。The length of value is greater than 65535.

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

備註

如果標頭中指定的標頭不存在,則Set方法會將新的標頭插入標頭名稱/值組的清單中。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.

如果中header指定的標頭已存在, value則會取代現有的值。If the header specified in header is already present, value replaces the existing value.

Set(String, String)

將指定的標頭設定為指定的值。Sets the specified header to the specified value.

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

參數

name
String

要設定的標頭。The header to set.

value
String

要設定的標頭內容。The content of the header to set.

例外狀況

namenullEmptyname is null or Empty.

value 的長度大於 65535。The length of value is greater than 65535.

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

-或--or- namevalue 包含無效字元。name or value contain invalid characters.

範例

下列範例會使用Set方法來設定現有標頭的值。The following example uses the Set method to set the value of an existing header.

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

   // Get the associated response for the above request.
   HttpWebResponse^ myHttpWebResponse = dynamic_cast<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 : {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 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);
}
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 in the request.
        myWebHeaderCollection.Set("Cache-Control", "no-cache")

        'Get the associated response for the above request.
        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        
    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 e As ArgumentException
        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

備註

如果標頭中指定的標頭不存在,則Set方法會將新的標頭插入標頭名稱/值組的清單中。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.

如果中header指定的標頭已存在, value則會取代現有的值。If the header specified in header is already present, value replaces the existing value.

適用於