WebHeaderCollection.Add メソッド

定義

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

オーバーロード

Add(String)

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

Add(HttpRequestHeader, String)

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

Add(HttpResponseHeader, String)

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

Add(String, String)

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

Add(String)

ソース:
WebHeaderCollection.cs
ソース:
WebHeaderCollection.cs
ソース:
WebHeaderCollection.cs

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

public:
 void Add(System::String ^ header);
public void Add (string header);
override this.Add : string -> unit
Public Sub Add (header As String)

パラメーター

header
String

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

例外

headernull または Empty です。

header に、コロン (:) 文字が含まれていません。

value の長さが 65535 を超えています。

- または -

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

- または -

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

- または -

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

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

次の例では、 メソッドを使用して、名前と値の WebHeaderCollection ペアを Add に追加します。

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;
   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 = 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( "\nWebException is thrown. \nMessage 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;

    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);
}
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
        
    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

注意

部分headerの長さ、つまりコロンの後の文字列 (:)) は、.NET Framework および .NET Core バージョン 2.0 から 3.1 でのみ検証されます。

  • 該当するすべての.NET Frameworkバージョンで: WebHeaderCollection部分headerの長さが 65535 を超える場合、プロパティによってHeaders返されるインスタンスは をArgumentOutOfRangeExceptionスローします。 その他 WebHeaderCollection のすべてのインスタンスは、任意の長さの を受け入れます。
  • バージョン 3.1 以降の .NET Core バージョンでは、 WebHeaderCollection部分headerの長さが 65535 より大きい場合、型HttpResponseHeaderの任意のヘッダーで使用されるインスタンスは をスローArgumentOutOfRangeExceptionします。 その他 WebHeaderCollection のすべてのインスタンスは、任意の長さの を受け入れます。
  • .NET 5 以降のバージョンでは、 は WebHeaderCollection 任意の長さの を受け入れます。

注釈

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

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

適用対象

Add(HttpRequestHeader, String)

ソース:
WebHeaderCollection.cs
ソース:
WebHeaderCollection.cs
ソース:
WebHeaderCollection.cs

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

public:
 void Add(System::Net::HttpRequestHeader header, System::String ^ value);
public void Add (System.Net.HttpRequestHeader header, string? value);
public void Add (System.Net.HttpRequestHeader header, string value);
override this.Add : System.Net.HttpRequestHeader * string -> unit
Public Sub Add (header As HttpRequestHeader, value As String)

パラメーター

header
HttpRequestHeader

コレクションに追加するヘッダー。

value
String

ヘッダーの内容。

例外

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

この WebHeaderCollection インスタンスは、HttpRequestHeader のインスタンスを許可しません。

注釈

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

指定したヘッダーが既に存在する場合は、 value がヘッダーに関連付けられている値のコンマ区切りのリストに追加されます。

注意

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

適用対象

Add(HttpResponseHeader, String)

ソース:
WebHeaderCollection.cs
ソース:
WebHeaderCollection.cs
ソース:
WebHeaderCollection.cs

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

public:
 void Add(System::Net::HttpResponseHeader header, System::String ^ value);
public void Add (System.Net.HttpResponseHeader header, string? value);
public void Add (System.Net.HttpResponseHeader header, string value);
override this.Add : System.Net.HttpResponseHeader * string -> unit
Public Sub Add (header As HttpResponseHeader, value As String)

パラメーター

header
HttpResponseHeader

コレクションに追加するヘッダー。

value
String

ヘッダーの内容。

例外

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

この WebHeaderCollection インスタンスは、HttpResponseHeader のインスタンスを許可しません。

注釈

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

指定したヘッダーが既に存在する場合は、 value がヘッダーに関連付けられている値のコンマ区切りのリストに追加されます。

注意

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

適用対象

Add(String, String)

ソース:
WebHeaderCollection.cs
ソース:
WebHeaderCollection.cs
ソース:
WebHeaderCollection.cs

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

public:
 override void Add(System::String ^ name, System::String ^ value);
public override void Add (string name, string? value);
public override void Add (string name, string value);
override this.Add : string * string -> unit
Public Overrides Sub Add (name As String, value As String)

パラメーター

name
String

コレクションに追加するヘッダー。

value
String

ヘッダーの内容。

例外

namenull または Empty です。または、無効な文字が含まれています。

- または -

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

- または -

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

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

次の例では、 メソッドを使用して、名前と値の WebHeaderCollection ペアを Add に追加します。

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;
   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 = 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( "\nWebException is thrown. \nMessage 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;

    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);
}
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
        
    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

注意

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

注釈

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

で指定されたヘッダーが既に name 存在する場合は、 value に関連付けられている name値のコンマ区切りの既存のリストに が追加されます。

適用対象