次の方法で共有


WebHeaderCollection.Add メソッド

定義

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

オーバーロード

名前 説明
Add(String)

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

Add(HttpRequestHeader, String)

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

Add(HttpResponseHeader, String)

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

Add(String, String)

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

Add(String)

ソース:
WebHeaderCollection.cs
ソース:
WebHeaderCollection.cs
ソース:
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 にはコロン (:)文字が含まれていません。

-又は-

headerの値部分の長さが 65535 を超えています。

-又は-

headerの名前部分がEmptyされているか、無効な文字が含まれています。

-又は-

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

-又は-

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

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

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

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 バージョン 2.0 ~ 3.1 でのみ検証されます。

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

注釈

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

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

適用対象

Add(HttpRequestHeader, String)

ソース:
WebHeaderCollection.cs
ソース:
WebHeaderCollection.cs
ソース:
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
ソース:
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
ソース:
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

ヘッダーの内容。

例外

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

-又は-

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

-又は-

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

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

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

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に関連付けられている値の既存のコンマ区切りリストに追加されます。

適用対象