2016-05-12 3 views
0

温度/ GPSデータをクラウドブローカーに送信するデバイスがあります。次に、ブローカは、「HTTPは、デバイスのデータJSONオブジェクトを選択したURLにポストします」(ブローカのサイトのドキュメントから引用します)。
この投稿をC#でどのように受け取って逆シリアル化しますか?私はnewtonsoft.jsonを使用しています。私はstackoverflowから同様の例を見てきましたが、私にとっては多くのエラーを作り出しています。私のコードは以下の通りです。 publicクラスのRootオブジェクトは、JSONを貼り付けることで作成されたので、すべて自動でした。最後の部分は、私が何をするべきか分からないものです(パブリッククラステストセクション)。私はどこに行くのか、それを適切にフォーマットする方法はわかりません。
ありがとうございます。C#でHTTP Post JSONを受信して​​使用するasp.net

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. 
     } 
    } 
} 

ありがとうございます。

+0

ブローカーがリクエストを送信するエンドポイントが必要ですか?または、いくつかのデータを取得するget要求を発行する必要がありますか? – peco

+0

エンドポイントが必要です。ブローカーにはルールがあり、モニターからデータを受信すると直ちにメッセージを送信します。 – bairdmar

答えて

0

あなたは単にポスト要求を受け入れるあなたの側にコントローラが必要な場合:

routes.MapHttpRoute(
    name: "API Default", 
    routeTemplate: "api/{controller}/{id}", 
    defaults: new { id = RouteParameter.Optional } 
); 
:デフォルトルートのテンプレートを使用してルートを設定している場合、これは api/myapiにPOSTリクエストを受け入れる

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

     return Ok(); 
    } 
} 

+0

うわー、これは完璧と思われます。私はチャンスを得るとすぐに私はしようとします。ありがとうございました! – bairdmar

+0

fiddler経由でlocalhostを完全にテストします。どうもありがとう。 – bairdmar

関連する問題