次の方法で共有


WebHeaderCollection.Add メソッド (String)

指定したヘッダーをコレクションに挿入します。

Overloads Public Sub Add( _
   ByVal header As String _)
[C#]
public void Add(stringheader);
[C++]
public: void Add(String* header);
[JScript]
public function Add(
   header : String);

パラメータ

  • header
    追加するヘッダー。コロンで名前と値を区切ります。

例外

例外の種類 条件
ArgumentNullException header が null 参照 (Visual Basic では Nothing) または Empty です。
ArgumentException header に、コロン (:) 文字が含まれていません。

または

header の名前部分が Empty です。または無効な文字が含まれています。

または

header が、プロパティで設定する必要がある制限付きヘッダーです。

または

header の値部分に無効な文字が含まれています。

解説

header パラメータは、"name:value" の形式で指定する必要があります。指定したヘッダーがコレクションに含まれなていない場合は、新しいヘッダーがコレクションに追加されます。

header で指定したヘッダーが既にコレクションに存在する場合、 header の値部分は既存の値と連結されます。

使用例

[Visual Basic, C#, C++] Add メソッドを使用して、名前/値ペアを WebHeaderCollection に追加する例を次に示します。

 
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
        
       Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method")
        
    'Add the Accept-Language header (for Danish) in the request.
        myWebHeaderCollection.Add("Accept-Language:da")
        
        'Include English in the Accept-Langauge header. 
        myWebHeaderCollection.Add("Accept-Language:en;q" + ChrW(61) + "0.8")
        
        'Get the associated response for the above request.
        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        
        'Print the headers for the request.
        printHeaders(myWebHeaderCollection)
        myHttpWebResponse.Close()
    'Catch exception if trying to add 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;

    Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method");

    //Add the Accept-Language header (for Danish) in the request.
    myWebHeaderCollection.Add("Accept-Language:da");

    //Include English in the Accept-Langauge header. 
    myWebHeaderCollection.Add("Accept-Language:en;q=0.8");

    //Get the associated response for the above request.
     HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();

    //Print the headers for the request.
    printHeaders(myWebHeaderCollection);
    myHttpWebResponse.Close();
}
//Catch exception if trying to add a restricted header.
catch(ArgumentException e) {
    Console.WriteLine(e.Message);
}
catch(WebException e) {
    Console.WriteLine("\nWebException is thrown. \nMessage 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;

   Console::WriteLine(S"Configuring Webrequest to accept Danish and English language using 'Add' method");

   //Add the Accept-Language header (for Danish) in the request.
   myWebHeaderCollection->Add(S"Accept-Language:da");

   //Include English in the Accept-Langauge header.
   myWebHeaderCollection->Add(S"Accept-Language:en;q=0.8");

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

   //Print the headers for the request.
   printHeaders(myWebHeaderCollection);
   myHttpWebResponse->Close();
}
//Catch exception if trying to add a restricted header.
catch(ArgumentException* e) {
   Console::WriteLine(e->Message);
} catch (WebException* e) {
   Console::WriteLine(S"\nWebException is thrown. \nMessage 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 名前空間 | WebHeaderCollection.Add オーバーロードの一覧