2016-10-07 19 views
0

我想使用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。

+1

只有一個'[FromBody]'參數允許(只有一個身體畢竟)。您可以使用兩個字符串屬性創建一個類併發送該類,或發送FormUrlEncoded內容而不是StringContent。 – Crowcoder

+0

謝謝!我會寫一個正式的答案,並讚揚你。 – Roka545

回答

1

想通了。感謝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; 
} 

現在你只需要使用一個動態類型訪問字符串1和字符串這樣的:

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

您應該至少處置HttpClient。 – Crowcoder

+0

不,你不應該處置HttpClient:https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ –

+0

另外:https://docs.microsoft.com/en-us/azure/架構/反模式/不當-實例化/ –

1

這不是唯一的方法,但應該工作。它假定使用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"; 
} 
相關問題