码迷,mamicode.com
首页 > Windows程序 > 详细

从kepware定时取web api内容

时间:2018-11-22 11:48:28      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:space   item   间隔   enabled   gate   字符串   ali   jar   请求   

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using Newtonsoft.Json.Linq;
 10 using Newtonsoft.Json;
 11 using System.Threading.Tasks;
 12 using System.Net.Sockets;
 13 using System.IO;
 14 using System.Net;
 15 using System.Timers;
 16 
 17 
 18 //从kepware取web api内容
 19 
 20 
 21 namespace kepware_winform_rec
 22 {
 23     public partial class Form1 : Form
 24     {
 25         public Form1()
 26         {
 27             InitializeComponent();
 28         }
 29 
 30         string[] nArray = new string[100];
 31 
 32         private void Form1_Load(object sender, EventArgs e)
 33         {
 34             
 35 
 36          //遍历本机的kep web api
 37             string ValueName = Get("http://192.168.1.9:39320/iotgateway/browse");
 38             JObject json1 = (JObject)JsonConvert.DeserializeObject(ValueName);
 39             JArray array = (JArray)json1["browseResults"];
 40             //MessageBox.Show(array.ToString());
 41             string aa = "";
 42             foreach (var jObject in array)
 43             {
 44                 //赋值属性
 45                 aa = jObject["id"].ToString();//获取字符串中id值
 46                 listBox1.Items.Add(aa);
 47             }
 48 
 49             for (int i = 0; i < listBox1.Items.Count; i++)
 50             {
 51                 nArray[i] = listBox1.GetItemText(i);
 52                
 53             }
 54 
 55             System.Timers.Timer maxTimer = new System.Timers.Timer();
 56             maxTimer.Elapsed += new ElapsedEventHandler(Tmr_Elapsed);
 57             // 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
 58             maxTimer.Interval = 3000;
 59             maxTimer.Enabled = true;
 60         }
 61 
 62         public void Rec()
 63         {
 64             try
 65             {
 66                 for (int i = 0; i < listBox1.Items.Count; i++)
 67                 {
 68                     string value = Get("http://127.0.0.1:39320/iotgateway/read?ids=" + listBox1.Items[i].ToString());
 69                     JObject json2 = (JObject)JsonConvert.DeserializeObject(value);
 70                     JArray array2 = (JArray)json2["readResults"];
 71                     //MessageBox.Show(json2.ToString());
 72                     //MessageBox.Show(array2.ToString());
 73                     string aa2 = "";
 74                     foreach (var jObject in array2)
 75                     {
 76                         //赋值属性
 77                         aa2 = jObject["v"].ToString();//获取字符串中id值
 78                         listBox2.Items.Add(aa2);
 79                     }
 80                 }
 81             }
 82             catch
 83             { }
 84         }
 85 
 86         private void Tmr_Elapsed(object sender, ElapsedEventArgs e)
 87         {
 88             listBox2.Items.Clear();
 89             Rec();
 90         }
 91         
 92 
 93 
 94         /// <summary>
 95         /// 指定Url地址使用Get 方式获取全部字符串
 96         /// </summary>
 97         /// <param name="url">请求链接地址</param>
 98         /// <returns></returns>
 99         public static string Get(string url)
100         {
101             string result = "";
102             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
103             HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
104             Stream stream = resp.GetResponseStream();
105             try
106             {
107                 //获取内容
108                 using (StreamReader reader = new StreamReader(stream))
109                 {
110                     result = reader.ReadToEnd();
111                 }
112             }
113             finally
114             {
115                 stream.Close();
116             }
117             return result;
118         }
119 
120         private void button1_Click(object sender, EventArgs e)
121         {
122             Rec();
123         }
124 
125     }
126 }

 

从kepware定时取web api内容

标签:space   item   间隔   enabled   gate   字符串   ali   jar   请求   

原文地址:https://www.cnblogs.com/dXIOT/p/9999821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!