使用 HttpClient 和 HttpResponseMessage 的带有两个字符串的 PostAsync

PostAsync with two strings using HttpClient and HttpResponseMessage

我正在尝试使用 HttpClient 和 HttpResponseMessage 通过两个字符串执行 Http POST 调用。

这是我调用 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。

这不是唯一的方法,但应该可行。它假设使用了 Nuget 包 System.Net.Http v4.1.0,而不是您可以从参考中添加的程序集。

public async static Task<string> ConvertFiles(string s1, string s2, string webAddress)
{
    using(HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Accept.Cleare();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

        List<KeyValuePair<string,string>> formFields = new List<KeyValuePair<string,string>>();
        formFields.Add(new KeyValuePair<string,string>("s1", s1));
        formFields.Add(new KeyValuePair<string,string>("s2", s2));
        var formContent = new FormUrlEncodedContent(formFields);

        HttpResponseMessage response = await client.PostAsync(webAddress, formContent);
        string content = await response.Content.ReadAsStringAsync();
        Console.WriteLine("Result: " + content);
        return content;
    }
}

[HttpPost("/mshdf/import")]
public string ConvertFiles(FormDataCollection data)
{
    Console.WriteLine(data.Get("s1"));
    Console.WriteLine(data.Get("s2"));
    return "woo";
}

想通了。感谢 CrowCoder 的 post。

我最终制作了一本包含我的价值观的词典。

public async static Task<string> ConvertFiles(string s1, string s2, string webAddress)
{
    Dictionary<string, string> jsonValues = new Dictionary<string, string>();
    jsonValues.Add("string1", "anyStringValue1");
    jsonValues.Add("string2", "anyStringValue2");

    HttpClient client = new HttpClient();
    StringContent sc = new StringContent(JsonConvert.SerializeObject(jsonValues), UnicodeEncoding.UTF8, "application/json");
    HttpResponseMessage response = await client.PostAsync(webAddress, sc);
    string content = await response.Content.ReadAsStringAsync();
    Console.WriteLine("Result: " + content);
    return content;
}

现在您只需要使用动态类型来访问 string1 和 string2,如下所示:

[HttpPost("/mshdf/import")]
public string ConvertFiles([FromBody]dynamic jsonObject)
{
    Console.WriteLine("string1: " + jsonObject.string1);
    Console.WriteLine("string2: " + jsonObject.string2);
    return "woo";
}