简体   繁体   English

在C#asp.net中接收和使用HTTP Post JSON

[英]receive and use HTTP Post JSON in C# asp.net

I have a device posting temperature/GPS data to a cloud broker. 我有一个设备向云代理发布温度/ GPS数据。 The broker then "HTTP Posts device data JSON objects to a URL of your choosing" (quoted from the broker site documentation). 然后,代理“HTTP将设备数据JSON对象发布到您选择的URL”(引自代理站点文档)。
How do I receive and deserialize this post in C#? 如何在C#中接收和反序列化这篇文章? I'm using newtonsoft.json. 我正在使用newtonsoft.json。 I've seen a similar example from stackoverflow, but for me its creating lots of errors. 我在stackoverflow中看过一个类似的例子,但对我来说它创造了很多错误。 My code is below. 我的代码如下。 public class Rootobject was created by pasting the JSON, so that was all automatic. 公共类Rootobject是通过粘贴JSON创建的,所以这一切都是自动的。 The part towards the end is what I don't know what to do with (public class testing section). 最终的部分是我不知道该怎么做(公共课测试部分)。 I'm not sure where it goes, and how to properly format it. 我不知道它到底在哪里,以及如何正确格式化它。
Thank you for any suggestions. 谢谢你的任何建议。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http;
using Newtonsoft.Json;



namespace WebServiceTest1.Models
{

        public class Rootobject
        {
            public DateTime received { get; set; }
            public string authtype { get; set; }
            public string[] tags { get; set; }
            public Routingresults routingResults { get; set; }
            public string device_name { get; set; }
            public int errorcode { get; set; }
            public string source { get; set; }
            public string timestamp { get; set; }
            public string record_id { get; set; }
            public string data { get; set; }
            public int device_id { get; set; }
        }

        public class Routingresults
        {
            public int matchedRules { get; set; }
            public object[] errors { get; set; }
        }

        //This is the part I dont know where to put, or how to use properly.  
        public class Testing
        { 
           string url = "http://example.com/MethodThatReturnsJson";
           //I'm sure this needs to be the URL of the site that is POSTING.

           WebClient client = new WebClient();

           string webServiceJsonString = client.DownloadString(url);
           //This line is erroring out.  doesn't like client.DownloadString(url)

           Rootobject yourObject = JsonConvert.DeserializeObject<Rootobject>(webServiceJsonString);
           //I typed Rootobject here based on class Rootobject above, but not confident that is correct.
        }
    }
}

Thank you. 谢谢。

If you simply need a controller on your side that will accept post requests: 如果你只需要一个可以接受邮寄请求的控制器:

public class MyApiController : ApiController
{
    public IHttpActionResult Post(Rootobject dto)
    {
        // do something...

        return Ok();
    }
}

This will accept post request to api/myapi if you have your routes setup with the default route template: 如果您使用默认路由模板设置路由,这将接受对api/myapi发布请求:

routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM