简体   繁体   English

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

[英]PostAsync with two strings using HttpClient and HttpResponseMessage

I'm trying to perform an Http POST call with two strings using HttpClient and HttpResponseMessage. 我正在尝试使用HttpClient和HttpResponseMessage用两个字符串执行Http POST调用。

Here's my code that calls the 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;
}

I believe I have to use StringContent, but I'm not sure how (which is why it is empty). 我相信我必须使用StringContent,但是我不确定如何使用(这就是为什么它为空)。

Here's my HttpPost call: 这是我的HttpPost呼叫:

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

I've just started using HttpClient and HttpResponseMessage. 我刚刚开始使用HttpClient和HttpResponseMessage。 Right now, the call doesn't occur - I'm guessing this is due to the fact that I'm not sending both strings s1 and s2 properly. 现在,该呼叫没有发生-我猜这是由于我没有正确发送字符串s1和s2所致。

This is not the only way but should work. 这不是唯一的方法,但应该起作用。 It assumes the Nuget package System.Net.Http v4.1.0 is used, not the assembly you can add from References. 假定使用了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";
}

Figured it out. 弄清楚了。 Thanks to CrowCoder for his post. 感谢CrowCoder的帖子。

I ended up making a Dictionary which contained my values. 我最终制作了包含我的价值观的字典。

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

Now you just need to use a dynamic type to access string1 and string2 like this: 现在,您只需要使用动态类型来访问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