WebHeaderCollection.Set メソッド

定義

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

オーバーロード

Set(HttpRequestHeader, String)

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

Set(HttpResponseHeader, String)

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

Set(String, String)

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

Set(HttpRequestHeader, String)

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

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)

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

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)

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

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

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

例外

namenull または Empty です。

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

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

- または -

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

次の例では、 メソッドを 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

Note

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

注釈

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

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

適用対象