我想使用HttpClient和HttpResponseMessage使用兩個字符串執行Http POST調用。使用HttpClient和HttpResponseMessage使用兩個字符串的PostAsync
這裏是我的代碼,它的帖子:
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]'參數允許(只有一個身體畢竟)。您可以使用兩個字符串屬性創建一個類併發送該類,或發送FormUrlEncoded內容而不是StringContent。 – Crowcoder
謝謝!我會寫一個正式的答案,並讚揚你。 – Roka545