當(dāng)前位置:首頁 > IT技術(shù) > Windows編程 > 正文

.NET對(duì)接視頻智能分析平臺(tái)API的技巧總結(jié)
2021-08-07 20:42:44

TSINGSEE青犀視頻智能分析平臺(tái)EasyNVR、EasyCVR等都具備豐富的API接口,API接口中密碼需要MD5加密,這一點(diǎn)是為了系統(tǒng)安全考慮,下文中有具體實(shí)現(xiàn),可以參考。本文我們總結(jié)下NET對(duì)接TSINGSEE青犀視頻智能分析平臺(tái)API用到的幾個(gè)小功能,TSINGSEE青犀視頻智能分析平臺(tái)中API接口提供的POST和GET的調(diào)用下文中也有具體實(shí)現(xiàn),請(qǐng)注意下,我們反復(fù)強(qiáng)調(diào)接口調(diào)用的時(shí)候請(qǐng)注意下TOKEN值的傳遞。

1、Easy系列產(chǎn)品在登錄時(shí)對(duì)密碼需要進(jìn)行32位MD5加密,.NET對(duì)應(yīng)實(shí)現(xiàn)如下:

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發(fā)送HTTP -GET方法的請(qǐng)求

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("發(fā)送GET請(qǐng)求出現(xiàn)異常:" + ex.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return result;
        }

3、向指定 URL 發(fā)送HTTP-POST方法的請(qǐng)求

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("發(fā)送GET請(qǐng)求出現(xiàn)異常:" + ex.Message);
            }
            return result;
        }

?

本文摘自 :https://blog.51cto.com/t

開通會(huì)員,享受整站包年服務(wù)立即開通 >