C # achieve a form accessible to OneNet cloud platform by Request, the data analysis show in the textbox, and to achieve data is automatically saved to MySQL data table
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();
}
Reference Links: https://www.cnblogs.com/luxiaoguogege/p/10142053.html