指定した名前と値のヘッダーをコレクションに挿入します。
Overrides Overloads Public Sub Add( _
ByVal name As String, _ ByVal value As String _)
[C#]
public override void Add(stringname,stringvalue);
[C++]
public: void Add(String* name,String* value);
[JScript]
public override function Add(
name : String,value : String);
パラメータ
- name
コレクションに追加するヘッダー。 - value
ヘッダーの内容。
例外
例外の種類 | 条件 |
---|---|
ArgumentException | name が null 参照 (Visual Basic では Nothing) または Empty です。あるいは、無効な文字が含まれています。
または name が、プロパティで設定する必要がある制限付きヘッダーです。 または value に無効な値が含まれています。 |
解説
name で指定したヘッダーが存在しない場合は、 Add メソッドによって、新しいヘッダーがヘッダーの名前/値ペアのリストに挿入されます。
name で指定したヘッダーが既に存在する場合は、 name に関連付けられている値の既存のリストに、 value が追加されます。このリストはコンマ区切りです。
使用例
[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 オーバーロードの一覧