C# http Post与Get方法控制继电器

时间:2022-06-01 16:42:40

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using SendNote; namespace NetworkRelay//此命名空间关于网络继电器 { class NetworkRelayControl//网络继电器控制类 { /* static void Main(string[] args) { string resultControl = "relay4=off&relay8=off"; while(resultControl!="$") { resultControl = Console.ReadLine(); HttpStatusCode stat = postHttpCMD("admin", "12345678", "192.168.1.166", resultControl, 2000);//发送控制命令 string result=getHttpStat("admin", "12345678", "192.168.1.166", 2000);//读取控制状态 string[] Resultsplite= result.Split(new char[] { ‘&‘ });//状态拆分 for (int i = 0; i < Resultsplite.Length; i++) { Console.WriteLine(Resultsplite[i]); } } } * */ #region 采用post方法发送命令控制网络继电器 public static HttpStatusCode postHttpCMD(string username, string pswd, string ip, string postDataStr,int timeout) { HttpStatusCode result=HttpStatusCode.ExpectationFailed; /*username为用户名 * pswd为密码 * url为post请求地址 * postDataStr为发送数据"relay1=on&relay2=off&relay3=on&relay4=off&relay5=on&relay6=on&relay7=off&relay8=on"表示继电器1:8的开关状态 * MsgEncode为字节编码 *timeout为http超时时间 *return 返回状态代码 */ if (string.IsNullOrEmpty(ip)) { LogHelper.WriteLog(typeof(NetworkRelayControl), "请求数据异常"+"时间" + DateTime.Now.ToString()); throw new ArgumentNullException("Url"); } try { string Url = "" + ip + "/relay.cgi";//访问继电器的URL LogHelper.WriteLogInfo(typeof(NetworkRelayControl), "访问地址为" + Url +"时间"+ DateTime.Now.ToString()); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);//创建url连接 request.PreAuthenticate = true; NetworkCredential net = new NetworkCredential(username, pswd);//添加用户名与密码 request.Credentials = net; byte[] data = Encoding.UTF8.GetBytes(postDataStr); request.Method = "POST";//设置请求方法 request.ContentType = "application/x-www-form-urlencoded";//请求格式 request.Timeout = timeout;//设置超时时间 Stream reqStream = request.GetRequestStream(); reqStream.Write(data, 0, data.Length);//把数据写入请求的数据流 HttpWebResponse response = (HttpWebResponse)request.GetResponse();//获取请求返回对象 reqStream.Close();//关闭请求的输入流 result = response.StatusCode; LogHelper.WriteLogInfo(typeof(NetworkRelayControl), "服务器状态代码为" + result + "时间" + DateTime.Now.ToString()); request.Abort();//释放请求资源 } catch (HttpListenerException e) { LogHelper.WriteLog(typeof(NetworkRelayControl), "Http异常,异常信息为" + e.Message + "时间" + DateTime.Now.ToString()); } catch (UriFormatException e) { LogHelper.WriteLog(typeof(NetworkRelayControl), "Uri异常,异常信息为" + e.Message + "时间" + DateTime.Now.ToString()); } catch (Exception e) { LogHelper.WriteLog(typeof(NetworkRelayControl), "请求异常,异常信息为" + e.Message + "时间" + DateTime.Now.ToString()); } return result; } #endregion #region 采用GET方法获取网络继电器的状态 public static string getHttpStat(string username,string pswd,string ip,int timeout)//使用get方法获取继电器的相关状态 { /* *username为用户名 * pswd 为用户密码 * ip 为ip地址 * return 字符串 relay1....8=on/off */ string result = String.Empty; string url = ""+ip +"/relaystat.cgi"; LogHelper.WriteLogInfo(typeof(NetworkRelayControl), "访问地址为" + url + "时间" + DateTime.Now.ToString()); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//创建url连接 request.PreAuthenticate = true; NetworkCredential net = new NetworkCredential(username, pswd);//添加用户名与密码 request.Credentials = net; request.ContentType = "text/html;charset=UTF-8";//请求类型 request.Method = "GET";//请求方法 request.Timeout = timeout;//设置请求超时时间 HttpWebResponse response = (HttpWebResponse)request.GetResponse();//获取返回对象 if(response.StatusCode.Equals(HttpStatusCode.OK)) { StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")); result = reader.ReadToEnd();//读取返回的数据 reader.Close();//关闭流 } request.Abort();//释放请求资源 }catch(Exception e) { LogHelper.WriteLog(typeof(NetworkRelayControl),"GET方法发生异常,异常信息为" + e.Message + " 时间:" + DateTime.Now.ToString()); } return result; } #endregion } }