WebHeaderCollection.Add 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将新标头插入集合中。
重载
| 名称 | 说明 |
|---|---|
| Add(String) |
将指定的标头插入集合中。 |
| Add(HttpRequestHeader, String) |
将具有指定值的指定标头插入到集合中。 |
| Add(HttpResponseHeader, String) |
将具有指定值的指定标头插入到集合中。 |
| Add(String, String) |
将具有指定名称和值的标头插入到集合中。 |
Add(String)
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- 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
要添加的标头,其名称和值用冒号分隔。
例外
header 是 null 或 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 和 .NET Core 版本 2.0 - 3.1 中验证。
- 在所有适用的 .NET Framework 版本上:WebHeaderCollection如果值部分
header的长度大于 65535,则属性返回Headers的实例将引发ArgumentOutOfRangeException该实例。 所有其他 WebHeaderCollection 实例接受任何长度 的值 。 - 在 .NET Core 版本到版本 3.1 上:WebHeaderCollection如果值部分
header的长度大于 65535,则与类型的任何标头HttpResponseHeader一起使用的实例将引发ArgumentOutOfRangeException。 所有其他 WebHeaderCollection 实例接受任何长度 的值 。 - 在 .NET 5 及更高版本中: WebHeaderCollection 接受任何长度 的值 。
注解
header必须以“name:value”格式指定参数。 如果集合中不存在指定的标头,则会向集合中添加一个新标头。
如果集合中 header 已存在指定的标头,则值部分 header 与现有值连接。
适用于
Add(HttpRequestHeader, String)
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- 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 则添加到与标头关联的值的逗号分隔列表中。
注释
仅在 .NET Framework 和 .NET Core 版本 2.0 - 3.1 中验证长度 value 。
- 在所有适用的 .NET Framework 版本上:WebHeaderCollection如果长度大于 65535,则Headers属性返回的
value实例将引发ArgumentOutOfRangeException。 所有其他 WebHeaderCollection 实例接受value任何长度。 - 在通过版本 3.1 的 .NET Core 版本中:WebHeaderCollection如果长度
value大于 65535,则与类型的任何标头一起使用的HttpResponseHeader实例将引发ArgumentOutOfRangeException。 所有其他 WebHeaderCollection 实例接受value任何长度。 - 在 .NET 5 及更高版本中: WebHeaderCollection 接受
value任何长度。
适用于
Add(HttpResponseHeader, String)
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- 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 则添加到与标头关联的值的逗号分隔列表中。
注释
仅在 .NET Framework 和 .NET Core 版本 2.0 - 3.1 中验证长度 value 。
- 在所有适用的 .NET Framework 版本上:WebHeaderCollection如果长度大于 65535,则Headers属性返回的
value实例将引发ArgumentOutOfRangeException。 所有其他 WebHeaderCollection 实例接受value任何长度。 - 在通过版本 3.1 的 .NET Core 版本中:WebHeaderCollection如果长度
value大于 65535,则与类型的任何标头一起使用的HttpResponseHeader实例将引发ArgumentOutOfRangeException。 所有其他 WebHeaderCollection 实例接受value任何长度。 - 在 .NET 5 及更高版本中: WebHeaderCollection 接受
value任何长度。
适用于
Add(String, String)
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- WebHeaderCollection.cs
- Source:
- 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
标头的内容。
例外
仅限 .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
注释
仅在 .NET Framework 和 .NET Core 版本 2.0 - 3.1 中验证长度 value 。
- 在所有适用的 .NET Framework 版本上:WebHeaderCollection如果长度大于 65535,则Headers属性返回的
value实例将引发ArgumentOutOfRangeException。 所有其他 WebHeaderCollection 实例接受value任何长度。 - 在通过版本 3.1 的 .NET Core 版本中:WebHeaderCollection如果长度
value大于 65535,则与类型的任何标头一起使用的HttpResponseHeader实例将引发ArgumentOutOfRangeException。 所有其他 WebHeaderCollection 实例接受value任何长度。 - 在 .NET 5 及更高版本中: WebHeaderCollection 接受
value任何长度。
注解
如果指定的 name 标头不存在,该方法会将 Add 新标头插入标头名称/值对列表中。
如果指定的 name 标头已存在, value 则会添加到与 name关联的值的现有逗号分隔列表中。