C# 当URL包含端口时,PostAsync不工作
我的.net应用程序尝试使用下面的代码访问外部APIC# 当URL包含端口时,PostAsync不工作,c#,asp.net-mvc,post,C#,Asp.net Mvc,Post,我的.net应用程序尝试使用下面的代码访问外部API using (var keyClient = new HttpClient()) { keyClient.BaseAddress = new Uri(ConfigurationManager.AppSettings["webshopurl"]); var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, str
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;
}
使用(var-keyClient=new-HttpClient())
{
keyClient.BaseAddress=新Uri(ConfigurationManager.AppSettings[“webshopurl”]);
var content=newformurlencodedcontent(new[]
{
新的KeyValuePair(“api_用户名”,
ConfigurationManager.AppSettings[“webshopAPIUserName”]),
新的KeyValuePair(“api_密码”,
ConfigurationManager.AppSettings[“webshopAPIPassword”])
});
var result=keyClient.PostAsync(“/api/v1/key.php”,content.).result;
token=result.Content.ReadAsStringAsync().result;
}
从本地计算机调用时,它工作正常。但当它托管在在线服务器URL中时,如http://app.test.net:5000/test
它没有调用API。如果我们拥有像http://app.test.net/test
它工作正常
原因是什么?为什么要使用
.Result
解压结果?使用wait
从async
方法获得结果要好得多。
.Result
如果不小心处理上下文,可能会导致死锁
斯蒂芬·克利里(Stephen Cleary)有一篇非常好的文章,详细介绍了更多细节
这看起来更像是一条评论,而不是一个答案。在远程服务器上时,如果您收到/记录了任何错误消息,您会收到什么错误消息。开始在那里寻找它为什么不调用API的线索。