共用方式為


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,請將集合物件轉換成具有StringString的 KeyValuePair 作為條件約束的IEnumerable < T >

屬性

Age

取得或設定 TimeSpan 物件,代表 HTTP 回應上 Age HTTP 標頭的值。

Allow

取得 HttpMethodHeaderValueCollectionHttpMethod 物件,代表 HTTP 回應上 Allow HTTP 標頭的值。

CacheControl

取得 物件的 HttpCacheDirectiveHeaderValueCollection ,這些物件代表 HTTP 回應上 Cache-Control HTTP 標頭的值。

Connection

取得 HttpConnectionOptionHeaderValueCollectionHttpConnectionOptionOptionHeaderValue 物件,這些物件代表 HTTP 回應上 Connection HTTP 標頭的值。

Date

取得或設定 DateTime 物件,表示 HTTP 回應上 Date HTTP 標頭的值。

Location

取得或設定 URI ,表示 HTTP 回應上的值或 位置 HTTP 標頭。

ProxyAuthenticate

取得 HttpChallengeHeaderValueCollectionHttpChallengeHeaderValue 物件,這些物件代表 HTTP 回應上 Proxy-Authenticate HTTP 標頭的值。

RetryAfter

取得或設定 HttpDateOrDeltaHeaderValue 物件,代表 HTTP 回應上 Retry-After HTTP 標頭的值。

Size

取得 HttpResponseHeaderCollection中的物件數目。

TransferEncoding

取得 HttpTransferCodingHeaderValueCollectionHttpTransferCodingHeaderValue 物件,這些物件代表 HTTP 回應上 Transfer-Encoding HTTP 標頭的值。

WwwAuthenticate

取得 HttpChallengeHeaderValueCollection的 HttpChallengeHeaderValue 物件,這些物件代表 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 ,而不需驗證。

適用於

另請參閱