HttpRequestHeaderCollection 類別

定義

提供與 HTTP 要求相關聯的 HTTP 標頭集合。

public ref class HttpRequestHeaderCollection 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 HttpRequestHeaderCollection 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 HttpRequestHeaderCollection 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 HttpRequestHeaderCollection : 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 HttpRequestHeaderCollection : IDictionary<string,string>, IEnumerable<KeyValuePair<string,string>>, IStringable
Public NotInheritable Class HttpRequestHeaderCollection
Implements IDictionary(Of String, String), IEnumerable(Of KeyValuePair(Of String, String)), IStringable
繼承
Object Platform::Object IInspectable HttpRequestHeaderCollection
屬性
實作
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 引進)

範例

下列範例程式碼示範使用 HttpRequestHeaderCollection 物件上的屬性,在 HttpRequestMessage 物件上取得和設定要求標頭的方法。 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;

        public void DemonstrateHeaderRequest()
        {
            DemonstrateHeaderRequestAccept();
            DemonstrateHeaderRequestAcceptEncoding();
            DemonstrateHeaderRequestAcceptLanguage();
            DemonstrateHeaderRequestAuthorization();
            DemonstrateHeaderRequestCacheControl();
            DemonstrateHeaderRequestConnection();
            DemonstrateHeaderRequestCookie();
            DemonstrateHeaderRequestDate();
            DemonstrateHeaderRequestFrom();
            DemonstrateHeaderRequestHost();
            DemonstrateHeaderRequestIfModifiedSince();
            DemonstrateHeaderRequestIfUnmodifiedSince();
            DemonstrateHeaderRequestMaxForwards();
            DemonstrateHeaderRequestProxyAuthorization();
            DemonstrateHeaderRequestReferer();
            DemonstrateHeaderRequestUserAgent();
        }

        public void DemonstrateHeaderRequestAccept()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.Accept.TryParseAdd ("audio/*");
            parsedOk = request.Headers.Accept.TryParseAdd ("audio/*; q=0.2");
            parsedOk = request.Headers.Accept.TryParseAdd ("audio/*; q=0.4; mysetting=myvalue");

            // Set the header with a strong type.
            request.Headers.Accept.Add(new HttpMediaTypeWithQualityHeaderValue("audio/*", .6));

            // Get the strong type out
            foreach (var value in request.Headers.Accept)
            {
                System.Diagnostics.Debug.WriteLine("One of the Accept values: {0}={1}", value.MediaType, value.Quality);
            }

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

        public void DemonstrateHeaderRequestAcceptEncoding()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.AcceptEncoding.TryParseAdd("compress");
            parsedOk = request.Headers.AcceptEncoding.TryParseAdd("gzip;q=1.0");

            // Set the header with a strong type.
            request.Headers.AcceptEncoding.Add(new HttpContentCodingWithQualityHeaderValue("*", 0));

            // Get the strong type out
            foreach (var value in request.Headers.AcceptEncoding)
            {
                System.Diagnostics.Debug.WriteLine("One of the AcceptEncoding values: {0}={1}", value.ContentCoding, value.Quality);
            }

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

        public void DemonstrateHeaderRequestAcceptLanguage()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.AcceptLanguage.TryParseAdd("da");
            parsedOk = request.Headers.AcceptLanguage.TryParseAdd("en-gb;q=0.8");

            // Set the header with a strong type.
            request.Headers.AcceptLanguage.Add(new HttpLanguageRangeWithQualityHeaderValue("en", .7));

            // Get the strong type out
            foreach (var value in request.Headers.AcceptLanguage)
            {
                System.Diagnostics.Debug.WriteLine("One of the AcceptLanguage values: {0}={1}", value.LanguageRange, value.Quality);
            }

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

        public void DemonstrateHeaderRequestAuthorization()
        {
            var request = new HttpRequestMessage();

            // Set the header with a strong type.
            string username = "user";
            string password = "password";
            var buffer = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary (username + ":" + password, Windows.Security.Cryptography.BinaryStringEncoding.Utf8);
            string base64token = Windows.Security.Cryptography.CryptographicBuffer.EncodeToBase64String(buffer);
            request.Headers.Authorization = new HttpCredentialsHeaderValue("Basic", base64token);


            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("One of the Authorization values: {0}={1}", 
                request.Headers.Authorization.Scheme,
                request.Headers.Authorization.Token);

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

        public void DemonstrateHeaderRequestCacheControl()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.CacheControl.TryParseAdd("no-store");

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

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

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


        public void DemonstrateHeaderRequestConnection()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.Connection.TryParseAdd("close");

            // Set the header with a strong type.
            request.Headers.Connection.Add(new HttpConnectionOptionHeaderValue("cache-control"));

            // Get the strong type out
            foreach (var value in request.Headers.Connection)
            {
                System.Diagnostics.Debug.WriteLine("One of the Connection values: {0}", value.Token);
            }

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

        public void DemonstrateHeaderRequestCookie()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.Cookie.TryParseAdd("cookieName=cookieValue");

            // Set the header with a strong type.
            request.Headers.Cookie.Add(new HttpCookiePairHeaderValue("cookie2", "value2"));

            // Get the strong type out
            foreach (var value in request.Headers.Cookie)
            {
                System.Diagnostics.Debug.WriteLine("One of the Cookie values: {0}={1}", value.Name, value.Value);
            }

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


        public void DemonstrateHeaderRequestDate()
        {
            var request = new HttpRequestMessage();

            // This is not typically set with a string.

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

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

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

        public void DemonstrateHeaderRequestFrom()
        {
            var request = new HttpRequestMessage();

            // Set the header with a string.
            request.Headers.From = "person@example.com";

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("From value: {0}", request.Headers.From);
        }

        public void DemonstrateHeaderRequestHost()
        {
            var request = new HttpRequestMessage();

            // This is not typically set with a string.

            // Set the header with a strong type.
            // HostName is in the Windows.Networking namespace.
            var value = new Windows.Networking.HostName("example.com");
            request.Headers.Host = value;

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("Canonical Host name: {0}", request.Headers.Host.CanonicalName);

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

        public void DemonstrateHeaderRequestIfModifiedSince()
        {
            var request = new HttpRequestMessage();

            // This is not typically set with a string.

            // Set the header with a strong type.
            var value = DateTimeOffset.Now.AddDays(-1); // Since yesterday.
            request.Headers.IfModifiedSince = value;

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

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

        public void DemonstrateHeaderRequestIfUnmodifiedSince()
        {
            var request = new HttpRequestMessage();

            // This is not typically set with a string.

            // Set the header with a strong type.
            var value = DateTimeOffset.Now.AddDays(-1); // Since yesterday.
            request.Headers.IfUnmodifiedSince = value;

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

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

        public void DemonstrateHeaderRequestMaxForwards()
        {
            var request = new HttpRequestMessage();

            // Set the header with an integer.
            request.Headers.MaxForwards= 2;

            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("MaxForwards value: {0}", request.Headers.MaxForwards);
        }

        public void DemonstrateHeaderRequestProxyAuthorization()
        {
            var request = new HttpRequestMessage();

            // Set the header with a strong type.
            string username = "user";
            string password = "password";
            var buffer = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary(username + ":" + password, Windows.Security.Cryptography.BinaryStringEncoding.Utf16LE);
            string base64token = Windows.Security.Cryptography.CryptographicBuffer.EncodeToBase64String(buffer);
            request.Headers.ProxyAuthorization = new HttpCredentialsHeaderValue("Basic", base64token);


            // Get the strong type out
            System.Diagnostics.Debug.WriteLine("One of the ProxyAuthorixation values: {0}={1}",
                request.Headers.ProxyAuthorization.Scheme,
                request.Headers.ProxyAuthorization.Token);

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


        public void DemonstrateHeaderRequestReferer()
        {
            var request = new HttpRequestMessage();

            // This is not typically set with a string.

            // Set the header with a strong type.
            // Uri is either in the Windows.Foundation namespace (JavaScript and C++)
            // or in the System.Net namespace (C#).
            var value = new Uri("http://example.com/");
            request.Headers.Referer = value;

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

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

        public void DemonstrateHeaderRequestUserAgent()
        {
            var request = new HttpRequestMessage();
            bool parsedOk = false;

            // Set the header with a string.
            parsedOk = request.Headers.UserAgent.TryParseAdd("testprogram/1.0");

            // Set the header with a strong type.
            request.Headers.UserAgent.Add(new HttpProductInfoHeaderValue("myprogram", "2.2"));

            // Get the strong type out
            foreach (var value in request.Headers.UserAgent)
            {
                System.Diagnostics.Debug.WriteLine("One of the UserAgent values: {0} / {1}", value.Product.Name, value.Product.Version);
            }

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

備註

HttpRequestHeaderCollection 是與 HTTP 要求相關聯的 HTTP 標頭集合。 HttpRequestHeaderCollection 物件可用來取得或設定 HTTP 要求上的特定標頭。 HttpRequestHeaderCollection 物件上的大部分屬性都提供特定 HTTP 標頭值的存取權。

HttpRequestMessage上的Headers屬性會傳回 HttpRequestHeaderCollection 物件。 HttpClient上的DefaultRequestHeaders屬性也會傳回 HttpRequestHeaderCollection 物件。 這些是建構 HttpRequestHeaderCollection 物件的兩種方法。

HttpRequestHeaderCollection 代表應用程式開發人員可以設定的 HTTP 要求上的 HTTP 標頭,而不是最終可能會隨著要求一起傳送的所有標頭。 HttpBaseProtocolFilter將會新增一些額外的標頭。

基礎堆疊可以變更 HTTP 要求上的標頭。 這就是應用程式在要求完成之後,可能會想要從 HttpRequestHeaderCollection 取得標頭值的原因。

列舉 C# 或 Microsoft Visual Basic 中的集合

您可以在 C# 或 Microsoft Visual Basic 中逐一查看 HttpRequestHeaderCollection 物件。 在許多情況下,例如使用 foreach 語法,編譯器會為您執行此轉換,而且您不需要明確地轉換成 IEnumerable 。 如果您需要明確轉換,例如,如果您想要呼叫GetEnumerator,請將集合物件轉換成具有StringString的 KeyValuePair 作為條件約束的IEnumerable < T >

屬性

Accept

取得 HttpMediaTypeWithQualityHeaderValueCollectionHttpMediaTypeWithQualityHeaderValue 物件,這些物件代表 HTTP 要求上 Accept HTTP 標頭的值。

AcceptEncoding

取得 HttpContentCodingWithQualityHeaderValueCollectionHttpContentCodingWithQualityHeaderValue 物件,這些物件代表 HTTP 要求上 Accept-Encoding HTTP 標頭的值。

AcceptLanguage

取得 HttpLanguageRangeWithQualityHeaderValueCollectionHttpLanguageRangeWithQualityHeaderValue 物件,這些物件代表 HTTP 要求上 Accept-Language HTTP 標頭的值。

Authorization

取得或設定 HttpCredentialsHeaderValue 物件,代表 HTTP 要求上 授權 HTTP 標頭的值。

CacheControl

取得 HttpCacheDirectiveHeaderValueCollection ,表示 HTTP 要求上 Cache-Control HTTP 標頭的值。

Connection

取得HttpConnectionOptionHeaderValueCollection 的 HttpConnectionOptionHeaderValue物件,這些物件代表 HTTP 要求上Connection HTTP 標頭的值。

Cookie

取得 HttpCookiePairHeaderValueCollectionHttpCookiePairHeaderValue 物件,這些物件代表 HTTP 要求上傳送的 Cookie HTTP 標頭值。

Date

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

Expect

取得 HttpExpectationHeaderValueCollectionHttpExpectationHeaderValue 物件,這些物件代表 HTTP 要求上 預期 HTTP 標頭的值。

From

取得或設定 String ,表示 HTTP 要求上 From HTTP 標頭的值。

Host

取得或設定 HostName ,表示 HTTP 要求上 主機 HTTP 標頭的值。

IfModifiedSince

取得或設定 DateTime 物件,表示 HTTP 要求上 If-Modified-Since HTTP 標頭的值。

IfUnmodifiedSince

取得或設定 DateTime 物件,表示 HTTP 要求上 If-Unmodified-Since HTTP 標頭的值。

MaxForwards

取得或設定整數值,表示 HTTP 要求上 Max-Forwards HTTP 標頭的值。

ProxyAuthorization

取得或設定 HttpCredentialsHeaderValue 物件,代表 HTTP 要求上 Proxy-Authorization HTTP 標頭的值。

Referer

取得或設定 URI ,表示 HTTP 要求上 參考者 HTTP 標頭的值。

Size

取得 HttpRequestHeaderCollection中的物件數目。

TransferEncoding

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

UserAgent

取得 HttpProductInfoHeaderValueCollection的 HttpProductInfoHeaderValue 物件,這些物件代表 HTTP 要求上 User-Agent HTTP 標頭的值。

方法

Append(String, String)

將新專案新增至 HttpRequestHeaderCollection的結尾。

Clear()

HttpRequestHeaderCollection移除所有物件。

First()

擷取反覆運算器至 HttpRequestHeaderCollection中的第一個專案。

GetView()

會傳回 HttpRequestHeaderCollection的不可變檢視。

HasKey(String)

判斷 HttpRequestHeaderCollection 是否包含指定的索引鍵。

Insert(String, String)

以指定的索引鍵和值插入或取代 HttpRequestHeaderCollection 中的專案。

Lookup(String)

如果存在 ,請在 HttpRequestHeaderCollection 中尋找專案。

Remove(String)

HttpRequestHeaderCollection移除特定物件。

ToString()

會傳回代表目前 HttpRequestHeaderCollection 物件的字串。

TryAppendWithoutValidation(String, String)

嘗試將指定的專案附加至 HttpRequestHeaderCollection 而不進行驗證。

適用於

另請參閱