using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Timers;
using System.Data;
using Newtonsoft;

namespace Tlink_Send
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("江苏xx电气公司OPC-domoticz转发工具");
            System.Timers.Timer maxTimer = new System.Timers.Timer();
            maxTimer.Elapsed += new ElapsedEventHandler(Tmr_Elapsed);
            maxTimer.Interval = 1000;
            maxTimer.Enabled = true;
            System.Threading.Thread.Sleep(1000);
            Console.ReadKey();

        }

        //idx 为设备id
        //svalue 为变量值

        static void Tmr_Elapsed(object sender, ElapsedEventArgs e)
        {
            GetWebClient("http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=" + "1" + "&nvalue=0&svalue=" + (System.DateTime.Now.Millisecond - 666).ToString());
            Console.WriteLine("成功转发");
            Console.ReadKey();
        }

        public static string GetWebClient(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Credentials = CredentialCache.DefaultCredentials;
            //获得用户名密码的Base64编码
            string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "admin", "admin")));
            //添加Authorization到HTTP头
            request.Headers.Add("Authorization", "Basic " + code);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string content = reader.ReadToEnd();
            return content;
        }
    }
}

 2020年3月10日

下面可以做一些灵活对接的工具,方便做项目。