概述
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
最后
以上就是妩媚毛衣为你收集整理的C#从OneNet获取数据的全部内容,希望文章能够帮你解决C#从OneNet获取数据所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复