HttpResponseHeaderCollection 类

定义

提供与 HTTP 响应关联的 HTTP 标头的集合。

public ref class HttpResponseHeaderCollection sealed : IIterable<IKeyValuePair<Platform::String ^, Platform::String ^> ^>, IMap<Platform::String ^, Platform::String ^>, IStringable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class HttpResponseHeaderCollection final : IIterable<IKeyValuePair<winrt::hstring, winrt::hstring const&>>, IMap<winrt::hstring, winrt::hstring const&>, IStringable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class HttpResponseHeaderCollection final : IIterable<IKeyValuePair<winrt::hstring, winrt::hstring const&>>, IMap<winrt::hstring, winrt::hstring const&>, IStringable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class HttpResponseHeaderCollection : IDictionary<string,string>, IEnumerable<KeyValuePair<string,string>>, IStringable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class HttpResponseHeaderCollection : IDictionary<string,string>, IEnumerable<KeyValuePair<string,string>>, IStringable
Public NotInheritable Class HttpResponseHeaderCollection
Implements IDictionary(Of String, String), IEnumerable(Of KeyValuePair(Of String, String)), IStringable
继承
Object Platform::Object IInspectable HttpResponseHeaderCollection
属性
实现
IDictionary<String,String> IMap<Platform::String,Platform::String> IMap<winrt::hstring,winrt::hstring> IIterable<IKeyValuePair<K,V>> IEnumerable<KeyValuePair<K,V>> IEnumerable<KeyValuePair<String,String>> IIterable<IKeyValuePair<Platform::String,Platform::String>> IIterable<IKeyValuePair<winrt::hstring,winrt::hstring>> IStringable

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)

示例

以下示例代码演示了一种方法,该方法使用 HttpResponseHeaderCollection 对象的属性获取和设置 HttpResponseMessage 对象上的响应标头。 Windows.Web.Http.Headers 命名空间具有许多适用于特定 HTTP 标头的强类型标头集合和值类,可用于通过验证获取和设置标头。

using System;
using System.Collections.Generic;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Web.Http;
using Windows.Web.Http.Headers;

        void DemonstrateResponseHeaders() {

            DemonstrateHeaderResponseAge();
            DemonstrateHeaderResponseAllow();
            DemonstrateHeaderResponseCacheControl();
            DemonstrateHeaderResponseDate();
            DemonstrateHeaderResponseLocation();
            DemonstrateHeaderResponseProxyAuthenticate();

        }


        public void DemonstrateHeaderResponseAge()
        {
            var response = new HttpResponseMessage();

            // Set the header with a strong type.
            DateTimeOffset value = DateTimeOffset.UtcNow;
            response.Headers.Age = new TimeSpan(1, 35, 55); // 1 hour, 35 minutes, 55 seconds.

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("Age value in minutes: {0}", response.Headers.Age.Value.TotalMinutes);

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The Age ToString() results: {0}", response.Headers.Age.ToString());
        }


        public void DemonstrateHeaderResponseAllow()
        {
            var response = new HttpResponseMessage();

            // Set the header with a string
            response.Headers.Allow.TryParseAdd ("GET");

            // Set the header with a strong type
            response.Headers.Allow.Add(HttpMethod.Patch);

            // Get the strong type out
            foreach (var value in response.Headers.Allow)
            {
                System.Diagnostics.Debug.WriteLine("Allow value: {0}", value.Method);
            }

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The Allow ToString() results: {0}", response.Headers.Allow.ToString());
        }

        public void DemonstrateHeaderResponseCacheControl()
        {
            var response = new HttpResponseMessage();

            // Set the header with a string
            response.Headers.CacheControl.TryParseAdd("public");

            // Set the header with a strong type
            response.Headers.CacheControl.Add(new HttpNameValueHeaderValue("max-age", "30"));

            // Get the strong type out
            foreach (var value in response.Headers.CacheControl)
            {
                System.Diagnostics.Debug.WriteLine("CacheControl {0}={1}", value.Name, value.Value);
            }

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The CacheControl ToString() results: {0}", response.Headers.CacheControl.ToString());
        }

        public void DemonstrateHeaderResponseDate()
        {
            var response = new HttpResponseMessage();

            // Set the header with a strong type.
            response.Headers.Date = DateTimeOffset.UtcNow;

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("Date value in ticks: {0}", response.Headers.Date.Value.Ticks);

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The Date ToString() results: {0}", response.Headers.Date.ToString());
        }

        public void DemonstrateHeaderResponseLocation()
        {
            var response = new HttpResponseMessage();

            // Set the header with a strong type.
            response.Headers.Location = new Uri("http://example.com/");

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("Location absolute uri: {0}", response.Headers.Location.AbsoluteUri);

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The Location ToString() results: {0}", response.Headers.Location.ToString());
        }

        public void DemonstrateHeaderResponseProxyAuthenticate()
        {
            var response = new HttpResponseMessage();

            // Set the header with a strong type.
            response.Headers.ProxyAuthenticate.TryParseAdd("Basic");
            response.Headers.ProxyAuthenticate.Add(new HttpChallengeHeaderValue("authScheme", "authToken"));

            // Get the strong type out
            foreach (var value in response.Headers.ProxyAuthenticate)
            {
                System.Diagnostics.Debug.WriteLine("Proxy authenticate scheme and token: {0} {1}", value.Scheme, value.Token);
            }

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The ProxyAuthenticate ToString() results: {0}", response.Headers.ProxyAuthenticate.ToString());
        }

        public void DemonstrateHeaderResponseRetryAfter()
        {
            var response = new HttpResponseMessage();

            // Set the header with a strong type.
            HttpDateOrDeltaHeaderValue newvalue;
            bool parseOk = HttpDateOrDeltaHeaderValue.TryParse("", out newvalue);
            if (parseOk)
            {
                response.Headers.RetryAfter = newvalue;
            }

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("Date value in ticks: {0}", response.Headers.Date.Value.Ticks);

            // The ToString() is useful for diagnostics, too.
            System.Diagnostics.Debug.WriteLine("The Date ToString() results: {0}", response.Headers.Date.ToString());
        }

注解

HttpResponseHeaderCollection 是与 HTTP 请求消息的 HTTP 响应关联的 HTTP 标头的集合。 HttpResponseHeaderCollection 对象可用于获取或设置 HTTP 响应上的特定标头。 HttpResponseHeaderCollection 对象上的大多数属性都提供对特定 HTTP 标头值的访问权限。

HttpResponseMessage 上的 Headers 属性返回 HttpResponseHeaderCollection 对象。 这是 HttpResponseHeaderCollection 的构造方式。

枚举 C# 或 Microsoft Visual Basic 中的集合

可以在 C# 或 Microsoft Visual Basic 中循环访问 HttpResponseHeaderCollection 对象。 在许多情况下,例如使用 foreach 语法,编译器会为你执行此强制转换,你无需显式转换为 IEnumerable 。 如果需要显式强制转换(例如,如果要调用 GetEnumerator),请将集合对象强制转换为 IEnumerable<T> ,其 KeyValuePairStringString 作为约束。

属性

Age

获取或设置 TimeSpan 对象,该对象代表 HTTP 响应上的 Age HTTP 标头的值。

Allow

获取 HttpMethod 对象的 HttpMethodHeaderValueCollection,这些对象表示 HTTP 响应上的 Allow HTTP 标头的值。

CacheControl

获取对象的 HttpCacheDirectiveHeaderValueCollection ,这些对象表示 HTTP 响应中 Cache-Control HTTP 标头的值。

Connection

获取 HttpConnectionOptionHeaderValue 对象的 HttpConnectionOptionHeaderValueCollection ,这些对象表示 HTTP 响应上的 Connection HTTP 标头的值。

Date

获取或设置 DateTime 对象,该对象代表 HTTP 响应中 Date HTTP 标头的值。

Location

获取或设置表示值或 HTTP 响应中位置 HTTP 标头的 Uri

ProxyAuthenticate

获取 HttpChallengeHeaderValue 对象的 HttpChallengeHeaderValueCollection ,这些对象表示 HTTP 响应中 代理身份验证 HTTP 标头的值。

RetryAfter

获取或设置 HttpDateOrDeltaHeaderValue 对象,该对象表示 HTTP 响应中 Retry-After HTTP 标头的值。

Size

获取 HttpResponseHeaderCollection 中的对象数。

TransferEncoding

获取 HttpTransferCodingHeaderValue 对象的 HttpTransferCodingHeaderValueCollection ,该对象表示 HTTP 响应中 Transfer-Encoding HTTP 标头的值。

WwwAuthenticate

获取 HttpChallengeHeaderValue 对象的 HttpChallengeHeaderValueCollection ,这些对象表示 HTTP 响应中 WWW-Authenticate HTTP 标头的值。

方法

Append(String, String)

将新项添加到 HttpResponseHeaderCollection 的末尾。

Clear()

从集合中删除所有对象。

First()

检索 HttpResponseHeaderCollection 中第一项的迭代器。

GetView()

返回 HttpResponseHeaderCollection 的不可变视图。

HasKey(String)

确定 HttpResponseHeaderCollection 是否包含指定的键。

Insert(String, String)

使用指定的键和值插入或替换 HttpResponseHeaderCollection 中的项。

Lookup(String)

HttpResponseHeaderCollection 中查找项。

Remove(String)

HttpResponseHeaderCollection 中删除具有给定键的项。

ToString()

返回表示当前 HttpResponseHeaderCollection 对象的字符串。

TryAppendWithoutValidation(String, String)

尝试在未验证的情况下将指定项追加到 HttpResponseHeaderCollection

适用于

另请参阅