2016-03-24 16 views
0

私はASP.NET Web API 2を使用しています。私のアプリケーションからは、いくつかの動的コンテンツを他のWeb APIサービスに投稿する必要があります。宛先サービスでは、この形式のデータが必要です。あるWeb APIサービスから別のWeb APIサービスへのPOST動的コンテンツ

public class DataModel 
{ 
    public dynamic Payload { get; set; } 
    public string id { get; set; } 
    public string key { get; set; } 
    public DateTime DateUTC { get; set; } 
} 

私はこのようなものを使用して考えています:

using (var client = new HttpClient()) 
{     
    client.BaseAddress = new Uri("http://localhost:9000/"); 
    client.DefaultRequestHeaders.Accept.Clear(); 
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
    dynamic payLoad = new ExpandoObject();  
    DataModel model = new DataModel(); 
    model.Payload = payLoad;  
    var response = await client.PostAsJsonAsync(url, model);  
} 

非同期の方法で別のウェブAPIサービスから動的な情報を投稿するための最良の方法は何ですか?あなたは、ほとんどがあった

答えて

0

...次のような

何かが、私はあなたの質問に読むことができますしたいためによると、あなたのために働く必要があります。

using (var client = new HttpClient()) 
{ 
    var url = new Uri("http://localhost:9000"); 
    client.DefaultRequestHeaders.Accept.Clear(); 
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
    dynamic payLoad = new ExpandoObject(); 

    DataModel model = new DataModel { Payload = payLoad }; 

    //Model will be serialized automatically. 
    var response = await client.PostAsJsonAsync(url, model); 

    //It may be a good thing to make sure that your request was handled properly 
    response.EnsureSuccessStatusCode(); 

    //If you need to work with the response, read its content! 
    //Here I implemented the controller so that it sends back what it received 
    //ReadAsAsync permits to deserialize the response content 
    var responseContent = await response.Content.ReadAsAsync<DataModel>(); 

    // Do stuff with responseContent... 
} 
関連する問題