2016-10-07 13 views
0

HttpClientとHttpResponseMessageを使用して2つの文字列でHTTP POST呼び出しを実行しようとしています。HttpClientとHttpResponseMessageを使用した2つの文字列による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を使用する必要があると考えているが、私は(それが空である理由である)かどうかはわかりません。私はただのHttpClientとHttpResponseMessage使用を開始しました

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

は、ここに私のHttpPost呼び出します。今、呼び出しは発生しません - 私はこれが両方の文字列s1とs2を適切に送信していないという事実によると推測しています。

+1

一つだけ '[FromBody]'パラメータは、(すべての後に一つだけの体があります)許可されています。 2つの文字列プロパティを持つクラスを作成して送信するか、StringContentの代わりにFormUrlEncodedコンテンツを送信することができます。 – 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と文字列2にアクセスするための動的型を使用する必要があります。

[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が使用されていて、Referencesから追加できるアセンブリではないことを前提としています。

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"; 
} 
関連する問題