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;
}
(C#)更改设备的Telnet功能,运用Http GET 和 PUT请求
最新推荐文章于 2021-02-06 13:41:04 发布