WebHeaderCollection.Add Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Inserts a new header into the collection.
Add(String) |
Inserts the specified header into the collection. |
Add(HttpRequestHeader, String) |
Inserts the specified header with the specified value into the collection. |
Add(HttpResponseHeader, String) |
Inserts the specified header with the specified value into the collection. |
Add(String, String) |
Inserts a header with the specified name and value into the collection. |
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
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)
Parameters
- header
- String
The header to add, with the name and value separated by a colon.
Exceptions
header
is null
or Empty.
header
does not contain a colon (:) character.
The length of value
is greater than 65535.
-or-
The name part of header
is Empty or contains invalid characters.
-or-
header
is a restricted header that should be set with a property.
-or-
The value part of header
contains invalid characters.
.NET Framework and .NET Core versions 2.0 - 3.1 only: The length of the string after the colon (:) is greater than 65535.
Examples
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
Nota
The length of the value portion of header
, that is, the string after the colon (:), is validated only in .NET Framework and .NET Core versions 2.0 - 3.1.
- On all applicable .NET Framework versions: A WebHeaderCollection instance that's returned by the Headers property will throw an ArgumentOutOfRangeException if the length of the value portion of
header
is greater than 65535. All other WebHeaderCollection instances accept a value of any length. - On .NET Core versions through version 3.1: A WebHeaderCollection instance used with any header of type HttpResponseHeader will throw an ArgumentOutOfRangeException if the length of the value portion of
header
is greater than 65535. All other WebHeaderCollection instances accept a value of any length. - On .NET 5 and later versions: WebHeaderCollection accepts a value of any length.
Remarks
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.
If the header specified in header
is already present in the collection, the value part of the header
is concatenated with the existing value.
Applies to
.NET 9 dan versi lain
Produk | Versi |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
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);
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)
Parameters
- header
- HttpRequestHeader
The header to add to the collection.
- value
- String
The content of the header.
Exceptions
.NET Framework and .NET Core versions 2.0 - 3.1 only: The length of value
is greater than 65535.
This WebHeaderCollection instance does not allow instances of HttpRequestHeader.
Remarks
If the specified header does not exist, the Add method inserts a new header into the list of header name/value pairs.
If the specified header is already present, value
is added to the comma-separated list of values associated with the header.
Nota
The length of value
is validated only in .NET Framework and .NET Core versions 2.0 - 3.1.
- On all applicable .NET Framework versions: A WebHeaderCollection instance that's returned by the Headers property will throw an ArgumentOutOfRangeException if the length of
value
is greater than 65535. All other WebHeaderCollection instances accept avalue
of any length. - On .NET Core versions through version 3.1: A WebHeaderCollection instance used with any header of type HttpResponseHeader will throw an ArgumentOutOfRangeException if the length of
value
is greater than 65535. All other WebHeaderCollection instances accept avalue
of any length. - On .NET 5 and later versions: WebHeaderCollection accepts a
value
of any length.
Applies to
.NET 9 dan versi lain
Produk | Versi |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
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);
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)
Parameters
- header
- HttpResponseHeader
The header to add to the collection.
- value
- String
The content of the header.
Exceptions
.NET Framework and .NET Core versions 2.0 - 3.1 only: The length of value
is greater than 65535.
This WebHeaderCollection instance does not allow instances of HttpResponseHeader.
Remarks
If the specified header does not exist, the Add method inserts a new header into the list of header name/value pairs.
If the specified header is already present, value
is added to the comma-separated list of values associated with the header.
Nota
The length of value
is validated only in .NET Framework and .NET Core versions 2.0 - 3.1.
- On all applicable .NET Framework versions: A WebHeaderCollection instance that's returned by the Headers property will throw an ArgumentOutOfRangeException if the length of
value
is greater than 65535. All other WebHeaderCollection instances accept avalue
of any length. - On .NET Core versions through version 3.1: A WebHeaderCollection instance used with any header of type HttpResponseHeader will throw an ArgumentOutOfRangeException if the length of
value
is greater than 65535. All other WebHeaderCollection instances accept avalue
of any length. - On .NET 5 and later versions: WebHeaderCollection accepts a
value
of any length.
Applies to
.NET 9 dan versi lain
Produk | Versi |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
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);
public override void Add (string name, string value);
override this.Add : string * string -> unit
Public Overrides Sub Add (name As String, value As String)
Parameters
- name
- String
The header to add to the collection.
- value
- String
The content of the header.
Exceptions
name
is null
, Empty, or contains invalid characters.
-or-
name
is a restricted header that must be set with a property setting.
-or-
value
contains invalid characters.
.NET Framework and .NET Core versions 2.0 - 3.1 only: The length of value
is greater than 65535.
Examples
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
Nota
The length of value
is validated only in .NET Framework and .NET Core versions 2.0 - 3.1.
- On all applicable .NET Framework versions: A WebHeaderCollection instance that's returned by the Headers property will throw an ArgumentOutOfRangeException if the length of
value
is greater than 65535. All other WebHeaderCollection instances accept avalue
of any length. - On .NET Core versions through version 3.1: A WebHeaderCollection instance used with any header of type HttpResponseHeader will throw an ArgumentOutOfRangeException if the length of
value
is greater than 65535. All other WebHeaderCollection instances accept avalue
of any length. - On .NET 5 and later versions: WebHeaderCollection accepts a
value
of any length.
Remarks
If the header specified in name
does not exist, the Add method inserts a new header into the list of header name/value pairs.
If the header specified in name
is already present, value
is added to the existing comma-separated list of values associated with name
.
Applies to
.NET 9 dan versi lain
Produk | Versi |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET maklum balas
.NET ialah projek sumber terbuka. Pilih pautan untuk memberikan maklum balas: