gpt4 book ai didi

c# - 从 Task 获取 HttpResponseMessage

转载 作者:行者123 更新时间:2023-11-30 23:22:49 28 4
gpt4 key购买 nike

在 HttpClient 内部使用语句我需​​要从某个地方解包 HttpResponseMessage。

using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authUser);
Task<HttpResponseMessage> m = client.GetAsync(url);
// HttpResponseMessage msg = ???
task.Wait();
return task.Result;
}

我的问题是:如何从这一行获取 HttpResponseMessage

Task<HttpResponseMessage> m = client.GetAsync(url);   

最佳答案

你应该await任务:

HttpResponseMessage m = await client.GetAsync(url);   

为此,需要将调用方法标记为async

关于c# - 从 Task<HttpResponseMessage> 获取 HttpResponseMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38633704/

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