TSINGSEE青犀视频智能分析平台EasyNVR、EasyCVR等都具备丰富的API接口,API接口中密码需要MD5加密,这一点是为了系统安全考虑,下文中有具体实现,可以参考。本文我们总结下NET对接TSINGSEE青犀视频智能分析平台API用到的几个小功能,TSINGSEE青犀视频智能分析平台中API接口提供的POST和GET的调用下文中也有具体实现,请注意下,我们反复强调接口调用的时候请注意下TOKEN值的传递。
1、Easy系列产品在登录时对密码需要进行32位MD5加密,.NET对应实现如下:
public string GetMd5(string str) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str); bytes = md5.ComputeHash(bytes); md5.Clear(); string ret = ""; for (int i = 0; i < bytes.Length; i++) { ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0'); } return ret.PadLeft(32, '0'); }
2、向指定URL发送HTTP -GET方法的请求
public string SendGet(string url, string param) { string result = String.Empty; StreamReader reader = null; try { string urlNameString = url + "?" + param; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlNameString); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; request.Accept = "*/*"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"; //头里面要带入token request.Headers.Add("Cookie", "token=acMyRwXZg"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); result = reader.ReadToEnd(); reader.Close(); responseStream.Close(); response.Close(); reader = null; responseStream = null; response = null; } catch (Exception ex) { Console.WriteLine("发送GET请求出现异常:" + ex.Message); } finally { if (reader != null) { reader.Close(); } } return result; }
3、向指定 URL 发送HTTP-POST方法的请求
public string SendPost(string url, string jsonData, string encoding) { string result = String.Empty; try { CookieContainer cookie = new CookieContainer(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Headers.Add("x-requested-with", "XMLHttpRequest"); request.ServicePoint.Expect100Continue = false; request.ContentType = "application/x-www-form-urlencoded"; request.Accept = "*/*"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"; request.ContentLength = Encoding.UTF8.GetByteCount(jsonData); request.CookieContainer = cookie; using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.GetEncoding(encoding))) { writer.Write(jsonData); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); response.Cookies = cookie.GetCookies(response.ResponseUri); using (Stream responseStream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding(encoding))) { result = reader.ReadToEnd(); reader.Close(); } responseStream.Close(); } response.Close(); response = null; request = null; } catch (Exception ex) { Console.WriteLine("发送GET请求出现异常:" + ex.Message); } return result; }
本文摘自 :https://blog.51cto.com/t