繁体   English   中英

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

[英]PostAsync with two strings using HttpClient and HttpResponseMessage

我正在尝试使用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所致。

这不是唯一的方法,但应该起作用。 假定使用了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的帖子。

我最终制作了包含我的价值观的字典。

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";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM