【发布时间】:2016-10-07 19:51:15
【问题描述】:
我正在尝试使用 HttpClient 和 HttpResponseMessage 执行带有两个字符串的 Http POST 调用。
这是我调用帖子的代码:
public async Task Convert()
{
string url = "http://localhost:5000/convert/files";
string stringValue1 = "test1";
string stringValue2 = "test2";
var x = await ConvertFiles(stringValue1, stringValue2, url);
}
public async static Task<string> ConvertFiles(string s1, string s2, string webAddress)
{
HttpClient client = new HttpClient();
StringContent sc = new StringContent("");
HttpResponseMessage response = await client.PostAsync(webAddress, sc);
string content = await response.Content.ReadAsStringAsync();
Console.WriteLine("Result: " + content);
return content;
}
我相信我必须使用 StringContent,但我不确定如何使用(这就是它为空的原因)。
这是我的 HttpPost 调用:
[HttpPost("/mshdf/import")]
public string ConvertFiles([FromBody]string s1, [FromBody]string s2)
{
Console.WriteLine("string1: " + s1);
Console.WriteLine("string2: " + s2);
return "woo";
}
我刚刚开始使用 HttpClient 和 HttpResponseMessage。现在,呼叫没有发生 - 我猜这是因为我没有正确发送字符串 s1 和 s2 。
【问题讨论】:
-
只允许一个
[FromBody]参数(毕竟只有一个body)。您可以创建一个具有两个字符串属性的类并发送它,或者发送 FormUrlEncoded 内容而不是 StringContent。 -
谢谢!我会写一个正式的答案并感谢你。
标签: c# parameters http-post httpclient httpresponse