次の方法で共有


WebHeaderCollection.Add メソッド

新しいヘッダーをコレクションに挿入します。

オーバーロードの一覧

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Sub Add(String)

[C#] public void Add(string);

[C++] public: void Add(String*);

[JScript] public function Add(String);

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Overrides Public Sub Add(String, String)

[C#] public override void Add(string, string);

[C++] public: void Add(String*, String*);

[JScript] public override function Add(String, String);

NameValueCollection から継承されます。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Sub Add(NameValueCollection)

[C#] public void Add(NameValueCollection);

[C++] public: void Add(NameValueCollection*);

[JScript] public function Add(NameValueCollection);

使用例

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

[Visual Basic, C#, C++] メモ   ここでは、Add のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

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