次の方法で共有


WebHeaderCollection.Set メソッド

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

Overrides Public Sub Set( _
   ByVal name As String, _   ByVal value As String _)
[C#]
public override void Set(stringname,stringvalue);
[C++]
public: void Set(String* name,String* value);
[JScript]
public override function Set(
   name : String,value : String);

パラメータ

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

例外

例外の種類 条件
ArgumentNullException name が null 参照 (Visual Basic では Nothing) または Empty です。
ArgumentException name が制限付きヘッダーです。

または

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

解説

ヘッダーで指定したヘッダーが存在しない場合は、 Set メソッドによって、新しいヘッダーがヘッダーの名前/値ペアのリストに挿入されます。

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

使用例

[Visual Basic, C#, C++] Set メソッドを使用して、既存のヘッダーの値を設定する例を次に示します。

 
Public Shared Sub Main()

Try
        'Create a web request for "www.msn.com".
        Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("https://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 'Main

[C#] 
try {
    // Create a web request for "www.msn.com".
     HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("https://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);
}

[C++] 
try {
   // Create a web request for S"www.msn.com".
   HttpWebRequest* myHttpWebRequest =
      dynamic_cast<HttpWebRequest*> (WebRequest::Create(S"https://www.msn.com"));

   // Get the headers associated with the request.
   WebHeaderCollection* myWebHeaderCollection = myHttpWebRequest->Headers;

   // Set the Cache-Control header in the request.
   myWebHeaderCollection->Set(S"Cache-Control", S"no-cache");

   // Get the associated response for the above request.
   HttpWebResponse* myHttpWebResponse =
      dynamic_cast<HttpWebResponse*> (myHttpWebRequest->GetResponse());

   Console::WriteLine (S"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(S"ArgumentException is thrown. Message is : {0}", e->Message);
} catch (WebException* e) {
   Console::WriteLine(S"WebException is thrown. Message is : {0}", e->Message);
   if (e->Status == WebExceptionStatus::ProtocolError) {
      Console::WriteLine(S"Status Code : {0}", __box((dynamic_cast<HttpWebResponse*>(e->Response))->StatusCode));
      Console::WriteLine(S"Status Description : {0}",
         (dynamic_cast<HttpWebResponse*>(e->Response))->StatusDescription);
      Console::WriteLine(S"Server : {0}",
         (dynamic_cast<HttpWebResponse*>(e->Response))->Server);
   }
} catch (Exception* e) {
   Console::WriteLine(S"Exception is thrown. Message is : {0}", e->Message);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

WebHeaderCollection クラス | WebHeaderCollection メンバ | System.Net 名前空間