HttpListenerResponse.AddHeader(String, String) 方法
定义
将指定的标头和值添加到此响应的 HTTP 标头。Adds the specified header and value to the HTTP headers for this response.
public:
void AddHeader(System::String ^ name, System::String ^ value);
public void AddHeader (string name, string value);
member this.AddHeader : string * string -> unit
Public Sub AddHeader (name As String, value As String)
参数
- name
- String
要设置的 HTTP 标头的名称。The name of the HTTP header to set.
- value
- String
name 标头的值。The value for the name header.
例外
name 为 null 或空字符串 ("")。name is null or an empty string ("").
不允许为指定的标头指定值。You are not allowed to specify a value for the specified header.
- 或 --or-
name 或 value 包含无效字符。name or value contains invalid characters.
仅限 .NET Framework 和 .NET Core 2.0 - 3.1 版本:value 的长度大于 65535 个字符。.NET Framework and .NET Core versions 2.0 - 3.1 only: The length of value is greater than 65,535 characters.
示例
下面的代码示例演示如何使用此属性添加标头。The following code example demonstrates adding a header using this property.
public static void SetExpirationDate(long seconds, HttpListenerResponse response)
{
response.AddHeader("Expires", seconds.ToString());
}
Public Shared Sub SetExpirationDate(ByVal seconds As Long, ByVal response As HttpListenerResponse)
response.AddHeader("Expires", seconds.ToString())
End Sub
注解
调用此方法等效于 Set 在由属性返回的集合上调用方法 Headers 。Calling this method is equivalent to calling the Set method on the collection returned by the Headers property.
如果中指定的标头 name 不存在,则此方法会将新的标头插入到 Headers 属性的集合中。If the header specified in name does not exist, this method inserts a new header into the Headers property's collection. 如果 name 集合中存在,则此方法会将现有值替换为 value 。If name is present in the collection, this method replaces the existing value with value. 若要将值添加到现有标头而不替换现有值,请使用 AppendHeader 方法。To add a value to an existing header without replacing the existing value, use the AppendHeader method.