WebHeaderCollection.Add WebHeaderCollection.Add WebHeaderCollection.Add WebHeaderCollection.Add Method

定義

新しいヘッダーをコレクションに挿入します。Inserts a new header into the collection.

オーバーロード

Add(String) Add(String) Add(String) Add(String)

指定したヘッダーをコレクションに挿入します。Inserts the specified header into the collection.

Add(HttpRequestHeader, String) Add(HttpRequestHeader, String) Add(HttpRequestHeader, String) Add(HttpRequestHeader, String)

指定した値を持つ、指定したヘッダーをコレクションに挿入します。Inserts the specified header with the specified value into the collection.

Add(HttpResponseHeader, String) Add(HttpResponseHeader, String) Add(HttpResponseHeader, String) Add(HttpResponseHeader, String)

指定した値を持つ、指定したヘッダーをコレクションに挿入します。Inserts the specified header with the specified value into the collection.

Add(String, String) Add(String, String) Add(String, String) Add(String, String)

指定した名前と値のヘッダーをコレクションに挿入します。Inserts a header with the specified name and value into the collection.

Add(String) Add(String) Add(String) Add(String)

指定したヘッダーをコレクションに挿入します。Inserts the specified header into the collection.

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

パラメーター

header
String String String String

追加するヘッダー。コロンで名前と値を区切ります。The header to add, with the name and value separated by a colon.

例外

header に、コロン (:) 文字が含まれていません。header does not contain a colon (:) character.

value の長さが 65535 を超えています。The length of value is greater than 65535.

または-or- header の名前部分が Empty です。または無効な文字が含まれています。The name part of header is Empty or contains invalid characters.

または-or- header が、プロパティで設定する必要がある制限付きヘッダーです。header is a restricted header that should be set with a property.

- または --or- header の値部分に無効な文字が含まれています。The value part of header contains invalid characters.

コロン (:) の後の文字列の長さが 65535 を超えています。The length the string after the colon (:) is greater than 65535.

次の例では、 WebHeaderCollection Addメソッドを使用して、に名前と値のペアを追加します。The following example adds a name/value pair to a WebHeaderCollection using the Add Method.

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 'Main

注釈

パラメーター headerは、"name: value" という形式で指定する必要があります。The header parameter must be specified in the format "name:value". 指定したヘッダーがコレクション内に存在しない場合は、新しいヘッダーがコレクションに追加されます。If the specified header does not exist in the collection, a new header is added to the collection.

header指定されたヘッダーがコレクション内に既に存在する場合は、 headerの値部分が既存の値と連結されます。If the header specified in header is already present in the collection, the value part of the header is concatenated with the existing value.

Add(HttpRequestHeader, String) Add(HttpRequestHeader, String) Add(HttpRequestHeader, String) Add(HttpRequestHeader, String)

指定した値を持つ、指定したヘッダーをコレクションに挿入します。Inserts the specified header with the specified value into the collection.

public:
 void Add(System::Net::HttpRequestHeader header, System::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 HttpRequestHeader HttpRequestHeader HttpRequestHeader

コレクションに追加するヘッダー。The header to add to the collection.

value
String String String String

ヘッダーの内容。The content of the header.

例外

value の長さが 65535 を超えています。The length of value is greater than 65535.

この WebHeaderCollection インスタンスは、HttpRequestHeader のインスタンスを許可しません。This WebHeaderCollection instance does not allow instances of HttpRequestHeader.

注釈

指定されたヘッダーが存在しないAdd場合、メソッドはヘッダーの名前と値のペアのリストに新しいヘッダーを挿入します。If the specified header does not exist, the Add method inserts a new header into the list of header name/value pairs.

指定したヘッダーが既に存在valueする場合、はヘッダーに関連付けられている値のコンマ区切りリストに追加されます。If the specified header is already present, value is added to the comma-separated list of values associated with the header.

Add(HttpResponseHeader, String) Add(HttpResponseHeader, String) Add(HttpResponseHeader, String) Add(HttpResponseHeader, String)

指定した値を持つ、指定したヘッダーをコレクションに挿入します。Inserts the specified header with the specified value into the collection.

public:
 void Add(System::Net::HttpResponseHeader header, System::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 HttpResponseHeader HttpResponseHeader HttpResponseHeader

コレクションに追加するヘッダー。The header to add to the collection.

value
String String String String

ヘッダーの内容。The content of the header.

例外

value の長さが 65535 を超えています。The length of value is greater than 65535.

この WebHeaderCollection インスタンスは、HttpResponseHeader のインスタンスを許可しません。This WebHeaderCollection instance does not allow instances of HttpResponseHeader.

注釈

指定されたヘッダーが存在しないAdd場合、メソッドはヘッダーの名前と値のペアのリストに新しいヘッダーを挿入します。If the specified header does not exist, the Add method inserts a new header into the list of header name/value pairs.

指定したヘッダーが既に存在valueする場合、はヘッダーに関連付けられている値のコンマ区切りリストに追加されます。If the specified header is already present, value is added to the comma-separated list of values associated with the header.

Add(String, String) Add(String, String) Add(String, String) Add(String, String)

指定した名前と値のヘッダーをコレクションに挿入します。Inserts a header with the specified name and value into the collection.

public:
 override void Add(System::String ^ name, System::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 String String String

コレクションに追加するヘッダー。The header to add to the collection.

value
String String String String

ヘッダーの内容。The content of the header.

例外

namenull または Empty です。または、無効な文字が含まれています。name is null, Empty, or contains invalid characters.

- または --or- name が、プロパティで設定する必要がある制限付きヘッダーです。name is a restricted header that must be set with a property setting.

または-or- value に無効な文字が含まれています。value contains invalid characters.

value の長さが 65535 を超えています。The length of value is greater than 65535.

次の例では、 WebHeaderCollection Addメソッドを使用して、に名前と値のペアを追加します。The following example adds a name/value pair to a WebHeaderCollection using the Add Method.

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 'Main

注釈

name指定されたヘッダーが存在しないAdd場合、メソッドはヘッダーの名前と値のペアのリストに新しいヘッダーを挿入します。If the header specified in name does not exist, the Add method inserts a new header into the list of header name/value pairs.

name指定されたヘッダーが既にvalue存在する場合、は、にname関連付けられている値の既存のコンマ区切りリストに追加されます。If the header specified in name is already present, value is added to the existing comma-separated list of values associated with name.

適用対象