HttpClient 的所有方法是否都在内部调用 SendAsync 方法?
Does all methods of HttpClient internally invoke SendAsync method?
是否 HttpClient
的所有方法,即 GetAsync
、PostAsync
等都在内部调用 SendAsync
方法?
是的,HttpClient
uses an HttpMessageHandler
underneath to perform all HTTP requests. The HttpMessageHandler
method Task<HttpResponseMessage> SendAsync(HttpRequestMessage, CancellationToken)
就是HttpClient
的叫法。
抽象classHttpMessageHandler
的默认实现是HttpClientHandler
。
You can pass in your own HttpMessageHandler
implementation to the HttpClient
constructor that takes one. 尽管您不太可能需要,但还是有应用程序的。例如,如果您想记录 HttpClient
发出的每个请求。您可以为 HttpMessageHandler
.
创建一个 LoggingHttpMessageHandler
装饰器
using (var handler = new HttpClientHandler())
using (var loggingHandler = new LoggingHttpClientHandler(handler, logger))
using (var client = new HttpClient(loggingHandler))
{
// Logs "GET https://www.google.com/"
var response = await client.GetAsync("https://www.google.com/");
...
}
是否 HttpClient
的所有方法,即 GetAsync
、PostAsync
等都在内部调用 SendAsync
方法?
是的,HttpClient
uses an HttpMessageHandler
underneath to perform all HTTP requests. The HttpMessageHandler
method Task<HttpResponseMessage> SendAsync(HttpRequestMessage, CancellationToken)
就是HttpClient
的叫法。
抽象classHttpMessageHandler
的默认实现是HttpClientHandler
。
You can pass in your own HttpMessageHandler
implementation to the HttpClient
constructor that takes one. 尽管您不太可能需要,但还是有应用程序的。例如,如果您想记录 HttpClient
发出的每个请求。您可以为 HttpMessageHandler
.
LoggingHttpMessageHandler
装饰器
using (var handler = new HttpClientHandler())
using (var loggingHandler = new LoggingHttpClientHandler(handler, logger))
using (var client = new HttpClient(loggingHandler))
{
// Logs "GET https://www.google.com/"
var response = await client.GetAsync("https://www.google.com/");
...
}