WebHeaderCollection.Set 方法

定义

将指定的标头设置为指定的值。

重载

Set(HttpRequestHeader, String)

将指定的标头设置为指定的值。

Set(HttpResponseHeader, String)

将指定的标头设置为指定的值。

Set(String, String)

将指定的标头设置为指定的值。

Set(HttpRequestHeader, String)

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

将指定的标头设置为指定的值。

public:
 void Set(System::Net::HttpRequestHeader header, System::String ^ value);
public void Set (System.Net.HttpRequestHeader header, 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 值。

value
String

要设置的标头内容。

例外

仅限 .NET Framework 和 .NET Core 2.0 - 3.1 版本:value 的长度大于 65535。

注解

如果标头中指定的标头不存在,该方法会将 Set 新标头插入标头名称/值对列表中。

如果 中 header 指定的标头已存在, value 则替换现有值。

注意

的长度value仅在 .NET Framework 和 .NET Core 版本 2.0 - 3.1 中验证。

适用于

Set(HttpResponseHeader, String)

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

将指定的标头设置为指定的值。

public:
 void Set(System::Net::HttpResponseHeader header, System::String ^ value);
public void Set (System.Net.HttpResponseHeader header, 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 值。

value
String

要设置的标头内容。

例外

仅限 .NET Framework 和 .NET Core 2.0 - 3.1 版本:value 的长度大于 65535。

注解

如果标头中指定的标头不存在,该方法会将 Set 新标头插入标头名称/值对列表中。

如果 中 header 指定的标头已存在, value 则替换现有值。

注意

的长度value仅在 .NET Framework 和 .NET Core 版本 2.0 - 3.1 中验证。

适用于

Set(String, String)

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

将指定的标头设置为指定的值。

public:
 override void Set(System::String ^ name, System::String ^ value);
public override void Set (string name, 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

要设置的标头。

value
String

要设置的标头内容。

例外

namenullEmpty

仅限 .NET Framework 和 .NET Core 2.0 - 3.1 版本:value 的长度大于 65535。

name 是受限制的标头。

- 或 -

namevalue 包含无效字符。

示例

以下示例使用 Set 方法设置现有标头的值。

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

注意

的长度value仅在 .NET Framework 和 .NET Core 版本 2.0 - 3.1 中验证。

注解

如果标头中指定的标头不存在,该方法会将 Set 新标头插入标头名称/值对列表中。

如果 中 header 指定的标头已存在, value 则替换现有值。

适用于