zoukankan      html  css  js  c++  java
  • 002转载----C# 基于OneNet 的物联网数据通信

    作者:lnwin521 
    来源:CSDN 
    原文:https://blog.csdn.net/lnwin521/article/details/84549606 
    (遇到404情况请复制粘贴后再打开)
    版权声明:本文为博主原创文章,转载请附上博文链接!

     

    首先非常感谢这位大佬的文章,对于连接onenet很有用。

     

    C# 基于OneNet 的物联网数据通信

    本篇简介包含两部分:1、数据向OneNet平台的上传;2、数据从OneNet平台的下载。

    1、数据向OneNet平台的上传

    1.1 上传数据采用POST方式:

     1  public void Post()
     2         {
     3             pictureBox1.BackColor = Color.Gray;
     4             string url = "http://api.heclouds.com/devices/503683965/datapoints?";
     5             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
     6             request.Method = "POST";
     7             SetHeaderValue(request.Headers, "api-key", "dTOsflbiN1YGgUjIgNYjY3TBigw=");//设备API地址和 首部参数
     8             request.Host = "api.heclouds.com";
     9             request.ProtocolVersion = new Version(1, 1);
    10             string Cod = "{"datastreams":[{"id":"temperature","datapoints":[{"value":"" + textBox4.Text + ""}]},{"id":"RH","datapoints":[{"value":"" + textBox5.Text + ""}]},{"id":"state","datapoints":[{"value":"" + textBox3.Text + ""}]}]}";
    11             byte[] data = Encoding.UTF8.GetBytes(Cod);
    12             request.ContentLength = data.Length;
    13             using (Stream reqStream = request.GetRequestStream())
    14             {
    15                 reqStream.Write(data, 0, data.Length);
    16                 reqStream.Close();
    17             }
    18 
    19             HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
    20             Stream stream = resp.GetResponseStream();
    21             //获取响应内容 
    22             using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
    23             {
    24                 textBox6.Text = reader.ReadToEnd();
    25                 pictureBox1.BackColor = Color.Lime;
    26             }
    27             // return result;
    28         }//面向OneNet的发送

     

    1.2 HTTP协议报文头编辑函数

     1  public static void SetHeaderValue(WebHeaderCollection header, string name, string value)// HTTP协议报文头加入
     2 
     3         {
     4             var property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.Instance | BindingFlags.NonPublic);
     5             if (property != null)
     6             {
     7                 var collection = property.GetValue(header, null) as NameValueCollection;
     8                 collection[name] = value;
     9             }
    10         }

     

    2、数据从OneNet平台的下载

    2.1 数据获取采用Get模式

     1  private void Get(object sender, EventArgs e)//连接服务器并获取数据
     2         {
     3             
     4             string url = "http://api.heclouds.com/devices/503683965/datapoints?";//设备地址
     5             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
     6             request.Method = "GET";
     7             SetHeaderValue(request.Headers, "api-key", "dTOsflbiN1YGgUjIgNYjY3TBigw=");//设备API地址和 首部参数
     8             request.Host = "api.heclouds.com";
     9             request.ProtocolVersion = new Version(1, 1);
    10             request.ContentType = "text/html;charset=UTF-8";
    11             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    12             Stream myResponseStream = response.GetResponseStream();
    13             StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
    14             string retString = myStreamReader.ReadToEnd();
    15             myStreamReader.Close();
    16             myResponseStream.Close();
    17             textBox5.Text = retString;
    18            
    19 
    20         }

     

    2.2 HTTP协议报文头编辑

     1  public static void SetHeaderValue(WebHeaderCollection header, string name, string value)// HTTP协议报文头加入
     2 
     3         {
     4             var property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.Instance | BindingFlags.NonPublic);
     5             if (property != null)
     6             {
     7                 var collection = property.GetValue(header, null) as NameValueCollection;
     8                 collection[name] = value;
     9             }
    10         }

     

     

    之前自己做的一个小程序,可以参考:https://download.csdn.net/download/lnwin521/10808227

     

     

     

    /********************************神奇的分割线********************************/

    那么当下载原文大佬的资源后,我们得到两个C#的源码

     

    1. 分析了Monitor里面的源码,在onenet中使用的是HTTP协议

    2. 将源码中的设备地址和秘钥替换成自己云端中去,程序中有两个的地方都要替换

    云端的:

    程序中的:

     

    3. 将源码中的一部分程序注销掉,因为我们云端没有这个东西,不然的话程序运行会出现错误

    1. 

    2. 

     

    3. 将源码注销之后运行点击自动运行就OK了,就可以看到我们云端的数据了

     

    4. 得到云端数据之后那就好办了,用我们以前学的知识来进行制作我们的应用软件

    忘记了的可以看。

    001_C#我的第一个串口上位机软件

     /********************************神奇的分割线********************************/

  • 相关阅读:
    MySql批量插入数据
    MyBatis批量插入
    poi读取合并单元格
    Vboxmanage改动uuid报错的解决的方法
    MySQL 删除数据库中反复数据(以部分数据为准)
    C3P0 APPARENT DEADLOCK
    使用Mybatis-Generator自己主动生成Dao、Model、Mapping相关文件
    [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)
    cocos2dx中关于Action动作的相关API的具体介绍
    时光轴三之 ExpandableListView版时光轴效果
  • 原文地址:https://www.cnblogs.com/luxiaoguogege/p/10142053.html
Copyright © 2011-2022 走看看