簡體   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