共用方式為


WebHeaderCollection.Set 方法

定義

將指定的標頭設定為指定的值。

多載

名稱 Description
Set(HttpRequestHeader, String)

將指定的標頭設定為指定的值。

Set(HttpResponseHeader, String)

將指定的標頭設定為指定的值。

Set(String, String)

將指定的標頭設定為指定的值。

Set(HttpRequestHeader, String)

來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
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。

這個 WebHeaderCollection 執行個體不允許 HttpRequestHeader 的執行個體。

備註

如果標頭中指定的標頭不存在, Set 方法會將新的標頭插入標頭名稱/值組清單中。

如果 中指定的 header 標頭已經存在, value 請取代現有的值。

注意

的長度 value 只會在 .NET Framework 和 .NET Core 2.0 - 3.1 版中驗證。

適用於

Set(HttpResponseHeader, String)

來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
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。

這個 WebHeaderCollection 執行個體不允許 HttpResponseHeader 的執行個體。

備註

如果標頭中指定的標頭不存在, Set 方法會將新的標頭插入標頭名稱/值組清單中。

如果 中指定的 header 標頭已經存在, value 請取代現有的值。

注意

的長度 value 只會在 .NET Framework 和 .NET Core 2.0 - 3.1 版中驗證。

適用於

Set(String, String)

來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
WebHeaderCollection.cs
來源:
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 "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 請取代現有的值。

適用於