gpt4 book ai didi

c# - 如何在没有异步的情况下使用 HttpClient

转载 作者:太空狗 更新时间:2023-10-29 20:56:13 24 4
gpt4 key购买 nike

你好,我正在关注 this guide

static async Task<Product> GetProductAsync(string path)
{
Product product = null;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
product = await response.Content.ReadAsAsync<Product>();
}
return product;
}

我在我的代码中使用了这个例子,我想知道有没有什么方法可以在没有 async/await 的情况下使用 HttpClient 以及我怎样才能只获得响应字符串?

提前致谢

最佳答案

is there any way to use HttpClient without async/await and how can I get only string of response?

HttpClient 专为异步使用而设计。

如果你想同步下载一个字符串,使用WebClient.DownloadString

关于c# - 如何在没有异步的情况下使用 HttpClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40208647/

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