Language

WebHeaderCollection.Set メソッド

定義

指定したヘッダーを指定した値に設定します。

オーバーロード

名前 説明
Set(HttpRequestHeader, String)

指定したヘッダーを指定した値に設定します。

Set(HttpResponseHeader, String)

指定したヘッダーを指定した値に設定します。

Set(String, String)

指定したヘッダーを指定した値に設定します。

Set(HttpRequestHeader, String)

ソース:
WebHeaderCollection.cs
ソース:
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置き換えられます。

Note

valueの長さは、.NET Framework および .NET Core バージョン 2.0 から 3.1 でのみ検証されます。

適用対象

Set(HttpResponseHeader, String)

ソース:
WebHeaderCollection.cs
ソース:
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置き換えられます。

Note

valueの長さは、.NET Framework および .NET Core バージョン 2.0 から 3.1 でのみ検証されます。

適用対象

Set(String, String)

ソース:
WebHeaderCollection.cs
ソース:
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

設定するヘッダーの内容。

例外

name が null または Empty。

.NET Framework および .NET Core バージョン 2.0 から 3.1 のみ: value の長さが 65535 を超えています。

name は制限付きヘッダーです。

-又は-

name または value に無効な文字が含まれています。

例

次の例では、 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

Note

valueの長さは、.NET Framework および .NET Core バージョン 2.0 から 3.1 でのみ検証されます。

注釈

ヘッダーに指定されたヘッダーが存在しない場合、 Set メソッドはヘッダー名と値のペアの一覧に新しいヘッダーを挿入します。

headerで指定されたヘッダーが既に存在する場合は、既存の値value置き換えられます。

適用対象