C#从OneNet获取数据

3 篇文章 2 订阅
1 篇文章 0 订阅

C#实现窗体通过Request访问到OneNet云平台,数据解析后展示在textbox中,并实现数据自动保存到MySQL数据表

private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://api.heclouds.com/devices/*****/datapoints?";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            var property = typeof(WebHeaderCollection).GetProperty("InnerCollection",                             
            BindingFlags.Instance | BindingFlags.NonPublic);
            if (property != null)
            {
                var collection = property.GetValue(request.Headers, null) as NameValueCollection;
                collection["api-key"] = "*******";
            }
            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();
            string temp = retString.Substring(99, 2);
            textBox1.Text = temp;
            string humi = retString.Substring(170, 1);
            textBox2.Text = humi;
            string beam = retString.Substring(240, 1);
            textBox3.Text = beam;
            string dateTime;
            dateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff");
            textBox4.Text = dateTime;
            string str = "data source=localhost;database=air;user id=root;password=123456;pooling=false;charset=utf8";
            MySqlConnection conn = new MySqlConnection(str);
            conn.Open();
            //数据插入MySQL数据表
            string sql = "Insert into air_value (temp,humi,beam,time) values ('" + temp + "','" + humi + "','" + beam + "','" + dateTime + "')";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.CommandType = CommandType.Text;
            MySqlDataReader sdr;
            sdr = cmd.ExecuteReader();
        
        }

参考链接: https://www.cnblogs.com/luxiaoguogege/p/10142053.html 

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值