gpt4 book ai didi

asp.net - HttpClient 的所有方法是否都在内部调用 SendAsync 方法?

转载 作者:行者123 更新时间:2023-12-05 03:12:34 25 4
gpt4 key购买 nike

HttpClient 的所有方法,即 GetAsyncPostAsync 等是否在内部调用 SendAsync 方法?

最佳答案

是的, HttpClient 使用 HttpMessageHandler 下面执行所有 HTTP 请求。 HttpMessageHandler方法 Task<HttpResponseMessage> SendAsync(HttpRequestMessage, CancellationToken) HttpClient 所称的.

抽象类的默认实现HttpMessageHandler HttpClientHandler .

You can pass in your own HttpMessageHandler implementation to the HttpClient constructor that takes one.尽管您不太可能需要,但还是有应用程序的。例如,如果您想记录每个请求您的 HttpClient使。你可以做一个 LoggingHttpMessageHandler HttpMessageHandler 的装饰器.

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/");
...
}

关于asp.net - HttpClient 的所有方法是否都在内部调用 SendAsync 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33434402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com