gpt4 book ai didi

c# - 当 URL 包含端口时,PostAsync 不起作用

转载 作者:行者123 更新时间:2023-11-30 12:41:42 24 4
gpt4 key购买 nike

我的 .net 应用程序尝试使用以下代码访问外部 API...

using (var keyClient = new HttpClient())
{
keyClient.BaseAddress = new Uri(ConfigurationManager.AppSettings["webshopurl"]);
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("api_username",
ConfigurationManager.AppSettings["webshopAPIUserName"]),
new KeyValuePair<string, string>("api_password",
ConfigurationManager.AppSettings["webshopAPIPassword"])
});
var result = keyClient.PostAsync("/api/v1/key.php", content).Result;
token = result.Content.ReadAsStringAsync().Result;
}

从本地机器调用时它工作正常。但是当它托管在诸如 http://app.test.net:5000/test 之类的在线服务器 URL 中时,它不会调用 API。如果我们托管像 http://app.test.net/test 这样的 URL,它就可以正常工作。

这是什么原因?

最佳答案

为什么要使用 .Result 来解压结果?使用 awaitasync 方法获取结果要好得多。如果您不注意上下文,.Result 可能会导致死锁。

Stephen Cleary 有一篇非常好的文章,其中介绍了更多细节。

Don't Block on Async Code

关于c# - 当 URL 包含端口时,PostAsync 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36790435/

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