接收和C#asp.net使用HTTP POST JSON [英] receive and use HTTP Post JSON in C# asp.net

查看:136
本文介绍了接收和C#asp.net使用HTTP POST JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个装置发帖温度/ GPS数据到云经纪人。该券商则HTTP帖子设备数据JSON对象,您选择的网址(从代理网站引用文档)。结果
如何接收和反序列化这个职位在C#中?我使用newtonsoft.json。我见过从计算器一个类似的例子,但对我来说它的创建大量的错误。我的code如下。公共类Rootobject被粘贴JSON创建的,所以这是所有自动。接近尾声的部分是什么,我不知道如何处理(公共类测试部分)做的。我不知道通向哪里,以及如何正确格式化。结果
谢谢你的任何建议。

I have a device posting temperature/GPS data to a cloud broker. The broker then "HTTP Posts device data JSON objects to a URL of your choosing" (quoted from the broker site documentation).
How do I receive and deserialize this post in C#? I'm using newtonsoft.json. I've seen a similar example from stackoverflow, but for me its creating lots of errors. My code is below. public class Rootobject was created by pasting the JSON, so that was all automatic. 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.
        }
    }
}

感谢您。

推荐答案

如果你只需要在你身边的控制器,将接受POST请求:

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

这将接受POST请求 API / myapi 如果你有你的路由设置的默认路由模板:

This will accept post request to api/myapi if you have your routes setup with the default route template:

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

这篇关于接收和C#asp.net使用HTTP POST JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆