HttpClientHandler クラス
定義
.NET Framework および .NET Core 2.0 以前の HttpClient で使用される既定のメッセージ ハンドラー。The default message handler used by HttpClient in .NET Framework and .NET Core 2.0 and earlier.
public ref class HttpClientHandler : System::Net::Http::HttpMessageHandler
public class HttpClientHandler : System.Net.Http.HttpMessageHandler
type HttpClientHandler = class
inherit HttpMessageHandler
Public Class HttpClientHandler
Inherits HttpMessageHandler
- 継承
- 派生
例
static async Task Main()
{
// Create an HttpClientHandler object and set to use default credentials
HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = true;
// Create an HttpClient object
HttpClient client = new HttpClient(handler);
// Call asynchronous network methods in a try/catch block to handle exceptions
try
{
HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
catch(HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ",e.Message);
}
// Need to call dispose on the HttpClient and HttpClientHandler objects
// when done using them, so the app doesn't leak resources
handler.Dispose();
client.Dispose();
}
前のコード例では、エントリポイントを使用して async Task Main()
います。The preceding code example uses an async Task Main()
entry point. この機能には、C# 7.1 以降が必要です。That feature requires C# 7.1 or later.
注釈
HttpClientHandler
クラスとその派生クラスを使用すると、開発者はプロキシから認証までのさまざまなオプションを構成できます。The HttpClientHandler
class and classes derived from it enable developers to configure a variety of options ranging from proxies to authentication.
.NET Core での HttpClientHandlerHttpClientHandler in .NET Core
.NET Core 2.1 以降では、クラスの実装は、 HttpClientHandler
クラスによって使用されるクロスプラットフォーム HTTP プロトコルスタックに基づいて変更されました System.Net.Http.SocketsHttpHandler 。Starting in .NET Core 2.1, the implementation of the HttpClientHandler
class was changed to be based on the cross-platform HTTP protocol stack used by the System.Net.Http.SocketsHttpHandler class. .NET Core 2.1 より前では、 HttpClientHandler
クラスは以前の HTTP プロトコルスタックを使用しています (Windows では、linux WinHttpHandler CurlHandler
のネイティブの libcurl コンポーネントの上に実装された内部クラスでもあります)。Prior to .NET Core 2.1, the HttpClientHandler
class used older HTTP protocol stacks (WinHttpHandler on Windows and CurlHandler
, an internal class implemented on top of Linux's native libcurl component, on Linux).
次の3つの方法のいずれかで、古い HTTP プロトコルスタックを使用するようにアプリを構成できます。You can configure your app to use the older HTTP protocol stacks in one of the following three ways:
メソッドを呼び出し AppContext.SetSwitch ます。Call the AppContext.SetSwitch method:
AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);
AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", False)
System.Net.Http.UseSocketsHttpHandler
構成ファイルの .netcore.runtimeconfig.js でスイッチを定義します。Define theSystem.Net.Http.UseSocketsHttpHandler
switch in the .netcore.runtimeconfig.json configuration file:"runtimeOptions": { "configProperties": { "System.Net.Http.UseSocketsHttpHandler": false } }
という名前の環境変数を定義
DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER
し、false
または 0 に設定します。Define an environment variable namedDOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER
and set it to eitherfalse
or 0.
コンストラクター
HttpClientHandler() |
HttpClientHandler クラスのインスタンスを作成します。Creates an instance of a HttpClientHandler class. |
プロパティ
AllowAutoRedirect |
ハンドラーがリダイレクト応答に従うかどうかを示す値を取得または設定します。Gets or sets a value that indicates whether the handler should follow redirection responses. |
AutomaticDecompression |
HTTP コンテンツ応答を自動展開するためにハンドラーによって使用される展開メソッドの種類を取得または設定します。Gets or sets the type of decompression method used by the handler for automatic decompression of the HTTP content response. |
CheckCertificateRevocationList |
証明書が証明機関の失効リストに対して照合されるかどうかを示す値を取得または設定します。Gets or sets a value that indicates whether the certificate is checked against the certificate authority revocation list. |
ClientCertificateOptions |
証明書を自動的に証明書ストアから取得するか、または呼び出し元が特定のクライアント証明書を渡すことを許可するかを示す値を、取得または設定します。Gets or sets a value that indicates if the certificate is automatically picked from the certificate store or if the caller is allowed to pass in a specific client certificate. |
ClientCertificates |
サーバーに関連付けられている要求であるセキュリティ証明書のコレクションを取得します。Gets the collection of security certificates that are associated requests to the server. |
CookieContainer |
サーバーの Cookie を保存するためにハンドラーによって使用されるコンテナーを取得または設定します。Gets or sets the cookie container used to store server cookies by the handler. |
Credentials |
このハンドラーで使用する認証情報を取得または設定します。Gets or sets authentication information used by this handler. |
DangerousAcceptAnyServerCertificateValidator |
常に |
DefaultProxyCredentials |
既定の (システム) プロキシが使用されている場合、認証のために既定のプロキシ サーバーに送信するための資格情報を取得または設定します。When the default (system) proxy is being used, gets or sets the credentials to submit to the default proxy server for authentication. 既定のプロキシは、UseProxy が |
MaxAutomaticRedirections |
ハンドラーがフォローするリダイレクトの最大数を取得または設定します。Gets or sets the maximum number of redirects that the handler follows. |
MaxConnectionsPerServer |
HttpClient オブジェクトを使用して要求を行うときに許可される (サーバー エンドポイントあたりの) コンカレント接続の最大数を取得または設定します。Gets or sets the maximum number of concurrent connections (per server endpoint) allowed when making requests using an HttpClient object. サーバー エンドポイントあたりの制限に注意してください。たとえば、値 256 は http://www.adatum.com/ に対して 256 のコンカレント接続数を許可し、http://www.adventure-works.com/ に対してさらに 256 のコンカレント接続数を許可します。Note that the limit is per server endpoint, so for example a value of 256 would permit 256 concurrent connections to http://www.adatum.com/ and another 256 to http://www.adventure-works.com/. |
MaxRequestContentBufferSize |
ハンドラーによって使用される最大要求コンテンツ バッファー サイズを取得または設定します。Gets or sets the maximum request content buffer size used by the handler. |
MaxResponseHeadersLength |
応答ヘッダーの最大長 (KB 単位、1 KB = 1024 バイト) を取得または設定します。Gets or sets the maximum length, in kilobytes (1024 bytes), of the response headers. たとえば、値が 64 の場合、応答ヘッダーの最大長に 65536 バイトが許可されます。For example, if the value is 64, then 65536 bytes are allowed for the maximum response headers' length. |
PreAuthenticate |
ハンドラーが要求と共に認証ヘッダーを送信するかどうかを示す値を取得または設定します。Gets or sets a value that indicates whether the handler sends an Authorization header with the request. |
Properties |
HttpClient 要求のカスタム プロパティの書き込み可能なディクショナリ (マップ) を取得します。Gets a writable dictionary (that is, a map) of custom properties for the HttpClient requests. ディクショナリが初期化されて空になると、カスタム ハンドラーと特殊な処理のキーと値のペアを挿入してクエリを実行できます。The dictionary is initialized empty; you can insert and query key-value pairs for your custom handlers and special processing. |
Proxy |
ハンドラーで使用するプロキシ情報を取得または設定します。Gets or sets proxy information used by the handler. |
ServerCertificateCustomValidationCallback |
サーバー証明書を検証するコールバック メソッドを取得または設定します。Gets or sets a callback method to validate the server certificate. |
SslProtocols |
HttpClientHandler オブジェクトによって管理される HttpClient オブジェクトが使用する TLS/SSL プロトコルを取得または設定します。Gets or sets the TLS/SSL protocol used by the HttpClient objects managed by the HttpClientHandler object. |
SupportsAutomaticDecompression |
ハンドラーが自動応答のコンテンツ展開をサポートするかどうかを示す値を取得します。Gets a value that indicates whether the handler supports automatic response content decompression. |
SupportsProxy |
ハンドルがプロキシ設定をサポートしているかどうかを示す値を取得します。Gets a value that indicates whether the handler supports proxy settings. |
SupportsRedirectConfiguration |
ハンドラーが AllowAutoRedirect および MaxAutomaticRedirections プロパティの構成設定をサポートするかどうかを示す値を取得します。Gets a value that indicates whether the handler supports configuration settings for the AllowAutoRedirect and MaxAutomaticRedirections properties. |
UseCookies |
ハンドラーが CookieContainer プロパティを使用してサーバーの Cookie を保存し、要求の送信時にそれらの Cookie を使用するかどうかを示す値を取得または設定します。Gets or sets a value that indicates whether the handler uses the CookieContainer property to store server cookies and uses these cookies when sending requests. |
UseDefaultCredentials |
既定の資格情報がハンドラーによる要求と共に送信されるかどうかを制御する値を取得または設定します。Gets or sets a value that controls whether default credentials are sent with requests by the handler. |
UseProxy |
ハンドラーが要求のプロキシを使用するかどうかを示す値を取得または設定します。Gets or sets a value that indicates whether the handler uses a proxy for requests. |
メソッド
Dispose() |
HttpMessageHandler が使用しているアンマネージド リソースを解放し、マネージド リソースを破棄します。Releases the unmanaged resources and disposes of the managed resources used by the HttpMessageHandler. (継承元 HttpMessageHandler) |
Dispose(Boolean) |
HttpClientHandler が使用しているアンマネージド リソースを解放します。オプションとして、マネージド リソースを破棄することもできます。Releases the unmanaged resources used by the HttpClientHandler and optionally disposes of the managed resources. |
Equals(Object) |
指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object. (継承元 Object) |
GetHashCode() |
既定のハッシュ関数として機能します。Serves as the default hash function. (継承元 Object) |
GetType() |
現在のインスタンスの Type を取得します。Gets the Type of the current instance. (継承元 Object) |
MemberwiseClone() |
現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object. (継承元 Object) |
Send(HttpRequestMessage, CancellationToken) |
HttpRequestMessage で指定された情報に基づいて HttpResponseMessage のインスタンスを作成します。Creates an instance of HttpResponseMessage based on the information provided in the HttpRequestMessage. |
Send(HttpRequestMessage, CancellationToken) |
派生クラスでオーバーライドされた場合は、指定した要求とキャンセル トークンを使用して HTTP 要求を送信します。When overridden in a derived class, sends an HTTP request with the specified request and cancellation token. それ以外の場合は、NotSupportedException をスローします。Otherwise, throws a NotSupportedException. (継承元 HttpMessageHandler) |
SendAsync(HttpRequestMessage, CancellationToken) |
HttpRequestMessage で提供される情報に基づいて、ブロックしない操作として HttpResponseMessage のインスタンスを作成します。Creates an instance of HttpResponseMessage based on the information provided in the HttpRequestMessage as an operation that will not block. |
ToString() |
現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object. (継承元 Object) |