(C#)更改设备的Telnet功能,运用Http GET 和 PUT请求

  public class telnetData_IPC
        {
            public int Enable { get; set; }
        }

        public class telnetData_NVR
        {
            public int Enabled { get; set; }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            TelnetSocket socket = new TelnetSocket();
            if(socket != null)
            {
                socket.Close();
                socket.Dispose();
                socket = null;
            }
            socket = new TelnetSocket();
            //socket.OnExceptionCaught += TelnetSocket.ExceptionCaught;
            //socket.OnDataReceived += TelnetSocket.DataReceived;

            string ip = "192.168.11.100";
            socket.Connect(ip);
            string str = socket.WaitFor("login:");
            socket.Write("admin" + "\r\n");
            str = socket.WaitFor("Password:");
            socket.Write("12345" + "\r\n");

            string str_tftp = "update -v" + "\r\n";
            socket.Write(str_tftp);


            socket.OnExceptionCaught += TelnetSocket.ExceptionCaught;
            socket.OnDataReceived += TelnetSocket.DataReceived;

             string version = socket.GetResponse();

        }

        //打开telnet功能
        private void button2_Click(object sender, EventArgs e)
        {
            string ipAddress = "192.168.11.123"; //ipc
            int devicetype = 2;//ipc
            //string ipAddress = "192.168.1.123"; //nvr
            bool tel_Result = Set_TelnetValue(ipAddress, devicetype,1,"admin","admin");
            string str = "open telnet ";
            if(tel_Result)
            {
                MessageBox.Show(str + "success!");
            }
            else
            {
                MessageBox.Show(str + "failed!");
            }
        }

        //关闭telnet功能
        private void button3_Click(object sender, EventArgs e)
        {
            string ipAddress = "192.168.11.123"; //ipc
            int devicetype = 2;//ipc
            //string ipAddress = "192.168.1.123"; //nvr
            bool tel_Result = Set_TelnetValue(ipAddress, devicetype, 0, "admin", "admin");
            string str = "close telnet ";
            if (tel_Result)
            {
                MessageBox.Show(str + "success!");
            }
            else
            {
                MessageBox.Show(str + "failed!");
            }
        }


        public static bool SetAllowUnsafeHeaderParsing20(bool useUnsafe)
        {
            //Get the assembly that contains the internal class
            System.Reflection.Assembly aNetAssembly = System.Reflection.Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
            if (aNetAssembly != null)
            {
                //Use the assembly in order to get the internal type for the internal class
                Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
                if (aSettingsType != null)
                {
                    //Use the internal static property to get an instance of the internal settings class.
                    //If the static instance isn't created allready the property will create it for us.
                    object anInstance = aSettingsType.InvokeMember("Section",
                      System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.NonPublic, null, null, new object[] { });

                    if (anInstance != null)
                    {
                        //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
                        System.Reflection.FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                        if (aUseUnsafeHeaderParsing != null)
                        {
                            aUseUnsafeHeaderParsing.SetValue(anInstance, useUnsafe);
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public bool Set_TelnetValue(string ipAddress, int devicetype, int telnetvalue, string username, string password)
        {
            bool tel_Result = false;
            if(devicetype ==1)
            {
                tel_Result = Set_TelnetValue_IPC(ipAddress, telnetvalue);
            }
            else if(devicetype ==2)
            {
                tel_Result = Set_TelnetValue_NVR(ipAddress, telnetvalue, username, password);
            }
            else
            {
                tel_Result = false;
            }
            return tel_Result;
        }

        public bool Set_TelnetValue_IPC(string ipAddress, int telnetvalue)
        {
            bool tel_Result = false;
            string URL = "http://" + ipAddress + "/LAPI/V1.0/System/Telnet";
            HttpWebRequest req_http = (HttpWebRequest)WebRequest.Create(URL);
            req_http.Method = "PUT";
            req_http.ContentType = "application/json";
            req_http.KeepAlive = true;
            req_http.Timeout = 10000;

            telnetData_IPC data_ipc = new telnetData_IPC();
            data_ipc.Enable = telnetvalue;
            string sb = new JavaScriptSerializer().Serialize(data_ipc);
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sb);
            req_http.ContentLength = bytes.Length;
            Stream cntstream = req_http.GetRequestStream();
            cntstream.Write(bytes, 0, bytes.Length);
            cntstream.Close();

            try
            {
                //获得响应
                string str = string.Empty;
                HttpWebResponse ponse_http = (HttpWebResponse)req_http.GetResponse();
                Stream stream = ponse_http.GetResponseStream();
                StreamReader sr = new StreamReader(stream);
                str = sr.ReadToEnd();
                string index = "\"StatusCode\": 0,";

                if (str.IndexOf(index) > -1)
                {
                    tel_Result = true;
                }
                else
                {
                    tel_Result = false;
                }
            }
            catch (Exception ex)
            {
                tel_Result = false;
            }
            return tel_Result;
        }

        public bool Set_TelnetValue_NVR(string ipAddress, int telnetvalue, string username, string password)
        {
            bool tel_Result = false;
            string URL = "http://" + ipAddress + "/LAPI/V1.0/System/Telnet";
            HttpWebRequest req_http = (HttpWebRequest)WebRequest.Create(URL);
            req_http.Method = "PUT";
            req_http.ContentType = "application/json";
            req_http.Credentials = new NetworkCredential(username, password);
            req_http.KeepAlive = true;
            req_http.Timeout = 10000;

            telnetData_NVR data_nvr = new telnetData_NVR();
            data_nvr.Enabled = telnetvalue;
            string sb = new JavaScriptSerializer().Serialize(data_nvr);
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sb);
            req_http.ContentLength = bytes.Length;
            Stream cntstream = req_http.GetRequestStream();
            cntstream.Write(bytes, 0, bytes.Length);
            cntstream.Close();

            SetAllowUnsafeHeaderParsing20(true);

            try
            {
                //获得响应
                string str = string.Empty;
                HttpWebResponse ponse_http = (HttpWebResponse)req_http.GetResponse();
                Stream stream = ponse_http.GetResponseStream();
                StreamReader sr = new StreamReader(stream);
                str = sr.ReadToEnd();
                string index = "\"StatusCode\":	0";
                if (str.IndexOf(index) > -1)
                {
                    tel_Result = true;
                }
                else
                {
                    tel_Result = false;
                }
            }
            catch (Exception ex)
            {
                tel_Result = false;
            }
           return tel_Result;
        }

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值