Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当URL包含端口时,PostAsync不工作_C#_Asp.net Mvc_Post - Fatal编程技术网

C# 当URL包含端口时,PostAsync不工作

C# 当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

我的.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;
}
使用(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的线索。