我知道有关于调用HttpPost方法的类似帖子,但我读过/实现的内容对我没用。我只是尝试进行POST调用,但由于某种原因,响应始终为null。我是Web开发和ASP的新手。
这是我的WebClient代码:
using (WebClient webClient = new WebClient())
{
webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
var dataToSend = "=testingu";
var response = webClient.UploadString("http://localhost:5000/core/test", "POST", dataToSend);
Console.WriteLine("Response is: " + response);
}
这是我的HttpPost代码:
[HttpPost("/core/test")]
public string PostObj([FromBody]dynamic input)
{
string result = "";
if (input == null)
System.Console.WriteLine("Input is null.");
else
System.Console.WriteLine("Input is not null: " + input);
return result;
}
每当调用PostObj时,"输入为空"当我期待看到"输入不为空时,总是执行行:testingu"打印。看起来我的WebClient代码是合理的,但我对此很新,所以任何帮助都会受到赞赏。