Share via


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 ヘッダーの値へのアクセスを提供します。

HttpRequestMessageHeaders プロパティは、HttpRequestHeaderCollection オブジェクトを返します。 HttpClient の DefaultRequestHeaders プロパティは 、HttpRequestHeaderCollection オブジェクトも返します。 これらは、HttpRequestHeaderCollection オブジェクトを構築する 2 つのメソッドです。

HttpRequestHeaderCollection は、アプリ開発者が設定できる HTTP 要求の HTTP ヘッダーを表します。最終的に要求と共に送信される可能性のあるすべてのヘッダーではありません。 HttpBaseProtocolFilter により、いくつかのヘッダーが追加されます。

HTTP 要求のヘッダーは、基になるスタックによって変更できます。 これは、要求が完了した後にアプリが HttpRequestHeaderCollection からヘッダー値を取得する必要がある理由です。

C# または Microsoft Visual Basic でのコレクションの列挙

C# または Microsoft Visual Basic で HttpRequestHeaderCollection オブジェクトを反復処理できます。 foreach 構文の使用など、多くの場合、コンパイラはこのキャストを行います。明示的に キャストするIEnumerable必要はありません。 GetEnumerator を呼び出す場合など、明示的にキャストする必要がある場合は、制約として StringStringKeyValuePair を指定してコレクション オブジェクトを IEnumerable<T> にキャストします。

プロパティ

Accept

HTTP 要求の Accept HTTP ヘッダーの値を表す HttpMediaTypeWithQualityHeaderValue オブジェクトの HttpMediaTypeWithQualityHeaderValueCollection を取得します。

AcceptEncoding

HTTP 要求の Accept-Encoding HTTP ヘッダーの値を表す HttpContentCodingWithQualityHeaderValue オブジェクトの HttpContentCodingWithQualityHeaderValueCollection を取得します。

AcceptLanguage

HTTP 要求の Accept-Language HTTP ヘッダーの値を表す HttpLanguageRangeWithQualityHeaderValue オブジェクトHttpLanguageRangeWithQualityHeaderValueCollection を取得します。

Authorization

HTTP 要求の Authorization HTTP ヘッダーの値を表す HttpCredentialsHeaderValue オブジェクトを取得または設定します。

CacheControl

HTTP 要求の Cache-Control HTTP ヘッダーの値を表す HttpCacheDirectiveHeaderValueCollection を取得します。

Connection

HTTP 要求の Connection HTTP ヘッダーの値を表す HttpConnectionOptionHeaderValue オブジェクトの HttpConnectionOptionHeaderValueCollection を取得します。

Cookie

HTTP 要求で送信される Cookie HTTP ヘッダーの値を表す HttpCookiePairHeaderValue オブジェクトの HttpCookiePairHeaderValueCollection を取得します。

Date

HTTP 要求の Date HTTP ヘッダーの値を表す DateTime オブジェクトを取得または設定します。

Expect

HTTP 要求の Expect HTTP ヘッダーの値を表す HttpExpectationHeaderValue オブジェクトの HttpExpectationHeaderValueCollection を取得します。

From

HTTP 要求の From HTTP ヘッダーの値を表す文字列型 (String) の値を取得または設定します。

Host

HTTP 要求の Host HTTP ヘッダーの値を表す HostName を取得または設定します。

IfModifiedSince

HTTP 要求の If-Modified-Since HTTP ヘッダーの値を表す DateTime オブジェクトを取得または設定します。

IfUnmodifiedSince

HTTP 要求の If-Unmodified-Since HTTP ヘッダーの値を表す DateTime オブジェクトを取得または設定します。

MaxForwards

HTTP 要求の Max-Forwards HTTP ヘッダーの値を表す整数値を取得または設定します。

ProxyAuthorization

HTTP 要求の Proxy-Authorization HTTP ヘッダーの値を表す HttpCredentialsHeaderValue オブジェクトを取得または設定します。

Referer

HTTP 要求の Referer HTTP ヘッダーの値を表す Uri を取得または設定します。

Size

HttpRequestHeaderCollection 内のオブジェクトの数を取得します。

TransferEncoding

HTTP 要求の Transfer-Encoding HTTP ヘッダーの値を表す HttpTransferCodingHeaderValue オブジェクトの HttpTransferCodingHeaderValueCollection を取得します。

UserAgent

HTTP 要求の User-Agent HTTP ヘッダーの値を表す HttpProductInfoHeaderValue オブジェクトの HttpProductInfoHeaderValueCollection を取得します。

メソッド

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 に追加してみてください。

適用対象

こちらもご覧ください