Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将动态内容从一个Web API服务发布到另一个Web API服务_C#_.net_Asp.net Mvc 4_Asp.net Web Api_Asp.net Web Api2 - Fatal编程技术网

C# 将动态内容从一个Web API服务发布到另一个Web API服务

C# 将动态内容从一个Web API服务发布到另一个Web API服务,c#,.net,asp.net-mvc-4,asp.net-web-api,asp.net-web-api2,C#,.net,Asp.net Mvc 4,Asp.net Web Api,Asp.net Web Api2,我正在使用ASP.NET Web API 2。在我的应用程序中,我需要将一些动态内容发布到另一个Web API服务。目标服务需要此格式的数据 public class DataModel { public dynamic Payload { get; set; } public string id { get; set; } public string key { get; set; } public DateTime DateUTC { get; set; }

我正在使用ASP.NET Web API 2。在我的应用程序中,我需要将一些动态内容发布到另一个Web API服务。目标服务需要此格式的数据

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

以异步方式将动态信息从一个Web API服务发布到另一个Web API服务的最佳方式是什么

根据我在你的问题中能读到的需要,下面的内容应该适合你

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...
}
使用(var-client=new-HttpClient())
{
var url=新Uri(“http://localhost:9000");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
动态有效负载=新的ExpandooObject();
数据模型模型=新数据模型{有效载荷=有效载荷};
//模型将自动序列化。
var response=await client.PostAsJsonAsync(url、模型);
//确保您的请求得到正确处理可能是一件好事
response.EnsureSuccessStatusCode();
//如果您需要处理响应,请阅读其内容!
//在这里,我实现了控制器,以便它发送回它接收到的内容
//ReadAsAsync允许反序列化响应内容
var responseContent=await response.Content.ReadAsAsync();
//用responseContent做一些事情。。。
}