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

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

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

1、数据向OneNet平台的上传

1.1 上传数据采用POST方式:

  public void Post()
        {
            pictureBox1.BackColor = Color.Gray;
            string url = "http://api.heclouds.com/devices/503683965/datapoints?";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            SetHeaderValue(request.Headers, "api-key", "dTOsflbiN1YGgUjIgNYjY3TBigw=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            string Cod = "{\"datastreams\":[{\"id\":\"temperature\",\"datapoints\":[{\"value\":\"" + textBox4.Text + "\"}]},{\"id\":\"RH\",\"datapoints\":[{\"value\":\"" + textBox5.Text + "\"}]},{\"id\":\"state\",\"datapoints\":[{\"value\":\"" + textBox3.Text + "\"}]}]}";
            byte[] data = Encoding.UTF8.GetBytes(Cod);
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }

            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容 
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                textBox6.Text = reader.ReadToEnd();
                pictureBox1.BackColor = Color.Lime;
            }
            // return result;
        }//面向OneNet的发送

1.2 HTTP协议报文头编辑函数

 public static void SetHeaderValue(WebHeaderCollection header, string name, string value)// HTTP协议报文头加入

        {
            var property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.Instance | BindingFlags.NonPublic);
            if (property != null)
            {
                var collection = property.GetValue(header, null) as NameValueCollection;
                collection[name] = value;
            }
        }

2、数据从OneNet平台的下载

2.1 数据获取采用Get模式

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

        }

2.2 HTTP协议报文头编辑

 public static void SetHeaderValue(WebHeaderCollection header, string name, string value)// HTTP协议报文头加入

        {
            var property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.Instance | BindingFlags.NonPublic);
            if (property != null)
            {
                var collection = property.GetValue(header, null) as NameValueCollection;
                collection[name] = value;
            }
        }

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

阅读更多

没有更多推荐了,返回首页