当前位置: 首页 > news >正文

海康平台对接关键类

文章目录

目录

前言

一、海康平台接口需要什么

二、使用步骤

总结



前言

最近对接海康平台,所以记录下需要用到的关键类

一、海康平台接口需要什么

海康平台接口需要appkey 和相关的 secret 这俩个

二、使用步骤

   //设置信息参数HttpUtillib.SetPlatformInfo(appkey, secret, ip, Convert.ToInt32(port));//post请求if (string.IsNullOrEmpty(url)){result.data = new List<DoorListResponse>();result.message = "暂未配置接口地址";return Json(result, serializerSettings, Encoding.UTF8);}var response = HttpUtillib.HttpPost(url, JsonConvert.SerializeObject(body));var resultData = Encoding.UTF8.GetString(response);

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Xhxx.Utils;
using Newtonsoft;
using System.Web.Helpers;
using Microsoft.Ajax.Utilities;
using Newtonsoft.Json;
using Org.BouncyCastle.Utilities.IO.Pem;namespace ES.Web.Api.ThirdPartyDocking.Haikang
{public class HttpUtillib{/// <summary>/// 设置信息参数/// </summary>/// <param name="appkey">合作方APPKey</param>/// <param name="secret">合作方APPSecret</param>/// <param name="ip">平台IP</param>/// <param name="port">平台端口,默认HTTPS的443端口</param>/// <param name="isHttps">是否启用HTTPS协议,默认HTTPS</param>/// <return></return>public static void SetPlatformInfo(string appkey, string secret, string ip, int port = 443, bool isHttps = true){_appkey = appkey;_secret = secret;_ip = ip;_port = port;_isHttps = isHttps;// 设置并发数,如不设置默认为2ServicePointManager.DefaultConnectionLimit = 512;}/// <summary>/// HTTP GET请求/// </summary>/// <param name="uri">HTTP接口Url,不带协议和端口,如/artemis/api/resource/v1/cameras/indexCode?cameraIndexCode=a10cafaa777c49a5af92c165c95970e0</param>/// <param name="timeout">请求超时时间,单位:秒</param>/// <returns></returns>public static byte[] HttpGet(string uri, int timeout){Dictionary<string, string> header = new Dictionary<string, string>();// 初始化请求:组装请求头,设置远程证书自动验证通过initRequest(header, uri, "", false);// build web request objectStringBuilder sb = new StringBuilder();sb.Append(_isHttps ? "https://" : "http://").Append(_ip).Append(":").Append(_port.ToString()).Append(uri);HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sb.ToString());req.KeepAlive = false;req.ProtocolVersion = HttpVersion.Version11;req.AllowAutoRedirect = false;   // 不允许自动重定向req.Method = "GET";req.Timeout = timeout * 1000;    // 传入是秒,需要转换成毫秒req.Accept = header["Accept"];req.ContentType = header["Content-Type"];foreach (string headerKey in header.Keys){if (headerKey.Contains("x-ca-")){req.Headers.Add(headerKey + ":" + header[headerKey]);}}HttpWebResponse rsp = null;try{rsp = (HttpWebResponse)req.GetResponse();if (HttpStatusCode.OK == rsp.StatusCode){Stream rspStream = rsp.GetResponseStream();     // 响应内容字节流StreamReader sr = new StreamReader(rspStream);string strStream = sr.ReadToEnd();long streamLength = strStream.Length;byte[] response = System.Text.Encoding.UTF8.GetBytes(strStream);rsp.Close();return response;}else if (HttpStatusCode.Found == rsp.StatusCode || HttpStatusCode.Moved == rsp.StatusCode)  // 302/301 redirect{string reqUrl = rsp.Headers["Location"].ToString();   // 获取重定向URLWebRequest wreq = WebRequest.Create(reqUrl);          // 重定向请求对象WebResponse wrsp = wreq.GetResponse();                // 重定向响应long streamLength = wrsp.ContentLength;               // 重定向响应内容长度Stream rspStream = wrsp.GetResponseStream();          // 响应内容字节流byte[] response = new byte[streamLength];rspStream.Read(response, 0, (int)streamLength);       // 读取响应内容至byte数组rspStream.Close();rsp.Close();return response;}rsp.Close();}catch (WebException e){if (rsp != null){rsp.Close();}}return null;}/// <summary>/// HTTP Post请求/// </summary>/// <param name="uri">HTTP接口Url,不带协议和端口,如/artemis/api/resource/v1/org/advance/orgList</param>/// <param name="body">请求参数</param>/// <param name="timeout">请求超时时间,单位:秒</param>/// <return>请求结果</return>public static byte[] HttpPost(string uri, string body, int timeout = 15){Dictionary<string, string> header = new Dictionary<string, string>();//初始化请求:组装请求头,设置远程证书自动验证通过initRequest(header, uri, body, true);//build web request objectStringBuilder sb = new StringBuilder();sb.Append(_isHttps ? "https://" : "http://").Append(_ip).Append(":").Append(_port.ToString()).Append(uri);//Log.CreateLog(Log.LOG_DEBUG, "", $"POST请求url:{sb.ToString()}", body);//创建POST请求HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(sb.ToString());req.KeepAlive = false;req.ProtocolVersion = HttpVersion.Version11;req.AllowAutoRedirect = false;   // 不允许自动重定向req.Method = "POST";req.Timeout = timeout * 1000;    // 传入是秒,需要转换成毫秒req.Accept = header["Accept"];req.ContentType = header["Content-Type"];foreach (string headerKey in header.Keys){if (headerKey.Contains("x-ca-")){req.Headers.Add(headerKey + ":" + header[headerKey]);}}//Log.CreateLog(Log.LOG_DEBUG, "", "POST请求url:", req.Headers.ToString());if (!string.IsNullOrWhiteSpace(body)){byte[] postBytes = Encoding.UTF8.GetBytes(body);req.ContentLength = postBytes.Length;Stream reqStream = null;try{reqStream = req.GetRequestStream();reqStream.Write(postBytes, 0, postBytes.Length);reqStream.Close();}catch (WebException e){if (reqStream != null){reqStream.Close();}return null;}}HttpWebResponse rsp = null;try{rsp = (HttpWebResponse)req.GetResponse();//Log.CreateLog(Log.LOG_DEBUG, "", $"POST返回:{rsp.StatusCode}", rsp.StatusDescription);if (HttpStatusCode.OK == rsp.StatusCode){Stream rspStream = rsp.GetResponseStream();StreamReader sr = new StreamReader(rspStream);string strStream = sr.ReadToEnd();long streamLength = strStream.Length;byte[] response = System.Text.Encoding.UTF8.GetBytes(strStream);rsp.Close();return response;}else if (HttpStatusCode.Found == rsp.StatusCode || HttpStatusCode.Moved == rsp.StatusCode)  // 302/301 redirect{try{string reqUrl = rsp.Headers["Location"].ToString();    // 如需要重定向URL,请自行修改接口返回此参数rsp.Close();WebRequest wreq = WebRequest.Create(reqUrl);rsp = (HttpWebResponse)wreq.GetResponse();Stream rspStream = rsp.GetResponseStream();long streamLength = rsp.ContentLength;int offset = 0;byte[] response = new byte[streamLength];while (streamLength > 0){int n = rspStream.Read(response, offset, (int)streamLength);if (0 == n){break;}offset += n;streamLength -= n;}return response;}catch (Exception e){rsp.Close();return null;}}rsp.Close();}catch (WebException e){Log.CreateLog(Log.LOG_DEBUG, "", $"WebException:{e.GetFullMessage()}");if (rsp != null){rsp.Close();}}return null;}private static void initRequest(Dictionary<string, string> header, string url, string body, bool isPost){// Accept                string accept = "application/json";// "*/*";header.Add("Accept", accept);// ContentType  string contentType = "application/json";header.Add("Content-Type", contentType);if (isPost){// content-md5,be careful it must be lower case.string contentMd5 = computeContentMd5(body);header.Add("content-md5", contentMd5);}// x-ca-timestampstring timestamp = ((DateTime.Now.Ticks - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)).Ticks) / 1000).ToString();header.Add("x-ca-timestamp", timestamp);// x-ca-noncestring nonce = System.Guid.NewGuid().ToString();header.Add("x-ca-nonce", nonce);// x-ca-keyheader.Add("x-ca-key", _appkey);// build string to signstring strToSign = buildSignString(isPost ? "POST" : "GET", url, header);string signedStr = computeForHMACSHA256(strToSign, _secret);// x-ca-signatureheader.Add("x-ca-signature", signedStr);if (_isHttps){// set remote certificate Validation auto passServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(remoteCertificateValidate);// FIX:修复不同.Net版对一些SecurityProtocolType枚举支持情况不一致导致编译失败等问题,这里统一使用数值ServicePointManager.SecurityProtocol = (SecurityProtocolType)48 | (SecurityProtocolType)3072 | (SecurityProtocolType)768 | (SecurityProtocolType)192;//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;}}/// <summary>/// 计算content-md5/// </summary>/// <param name="body"></param>/// <returns>base64后的content-md5</returns>private static string computeContentMd5(string body){MD5 md5 = new MD5CryptoServiceProvider();byte[] result = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(body));return Convert.ToBase64String(result);}/// <summary>/// 远程证书验证/// </summary>/// <param name="sender"></param>/// <param name="cert"></param>/// <param name="chain"></param>/// <param name="error"></param>/// <returns>验证是否通过,始终通过</returns>private static bool remoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error){return true;}/// <summary>/// 计算HMACSHA265/// </summary>/// <param name="str">待计算字符串</param>/// <param name="secret">平台APPSecet</param>/// <returns>HMAXH265计算结果字符串</returns>private static string computeForHMACSHA256(string str, string secret){var encoder = new System.Text.UTF8Encoding();byte[] secretBytes = encoder.GetBytes(secret);byte[] strBytes = encoder.GetBytes(str);var opertor = new HMACSHA256(secretBytes);byte[] hashbytes = opertor.ComputeHash(strBytes);return Convert.ToBase64String(hashbytes);}/// <summary>/// 计算签名字符串/// </summary>/// <param name="method">HTTP请求方法,如“POST”</param>/// <param name="url">接口Url,如/artemis/api/resource/v1/org/advance/orgList</param>/// <param name="header">请求头</param>/// <returns>签名字符串</returns>private static string buildSignString(string method, string url, Dictionary<string, string> header){StringBuilder sb = new StringBuilder();sb.Append(method.ToUpper()).Append("\n");if (null != header){if (null != header["Accept"]){sb.Append((string)header["Accept"]);sb.Append("\n");}if (header.Keys.Contains("Content-MD5") && null != header["Content-MD5"]){sb.Append((string)header["Content-MD5"]);sb.Append("\n");}if (null != header["Content-Type"]){sb.Append((string)header["Content-Type"]);sb.Append("\n");}if (header.Keys.Contains("Date") && null != header["Date"]){sb.Append((string)header["Date"]);sb.Append("\n");}}// build and add header to signstring signHeader = buildSignHeader(header);sb.Append(signHeader);sb.Append(url);return sb.ToString();}/// <summary>/// 计算签名头/// </summary>/// <param name="header">请求头</param>/// <returns>签名头</returns>private static string buildSignHeader(Dictionary<string, string> header){Dictionary<string, string> sortedDicHeader = new Dictionary<string, string>();sortedDicHeader = header;var dic = from objDic in sortedDicHeader orderby objDic.Key ascending select objDic;StringBuilder sbSignHeader = new StringBuilder();StringBuilder sb = new StringBuilder();foreach (KeyValuePair<string, string> kvp in dic){if (kvp.Key.Replace(" ", "").Contains("x-ca-")){sb.Append(kvp.Key + ":");if (!string.IsNullOrWhiteSpace(kvp.Value)){sb.Append(kvp.Value);}sb.Append("\n");if (sbSignHeader.Length > 0){sbSignHeader.Append(",");}sbSignHeader.Append(kvp.Key);}}header.Add("x-ca-signature-headers", sbSignHeader.ToString());return sb.ToString();}/// <summary>/// 平台ip/// </summary>private static string _ip;/// <summary>/// 平台端口/// </summary>private static int _port = 443;/// <summary>/// 平台APPKey/// </summary>private static string _appkey;/// <summary>/// 平台APPSecret/// </summary>private static string _secret;/// <summary>/// 是否使用HTTPS协议/// </summary>private static bool _isHttps = true;}
}

总结

以上就是使用海康平台对接的关键方法,至于其中具体参数怎么获取,我这边主要是将关键数据存在数据库表中获取

相关文章:

  • Ubuntu摄像头打开失败
  • 4.重建大师菜单栏介绍
  • Solana数据索引问题与解决方案
  • Nginx+Lua 实战避坑:从模块加载失败到版本冲突的深度剖析
  • spark中的转换算子
  • 固定步长和变步长的LMS自适应滤波器算法
  • Qwen集成clickhouse实现RAG
  • WK-F01和WK-F02以及WK-F01在电动三轮车和休闲三轮上的应用比较
  • JVM 与云原生的完美融合:引领技术潮流
  • Qt file文件操作详解
  • 密西根大学新作——LightEMMA:自动驾驶中轻量级端到端多模态模型
  • 服务器时间发生跳变导致hghac中对应主机状态频繁切换为crash或stop
  • OSS-承载数据的巨轮
  • Qt功能区:简介与安装
  • MCU ESP32-S3+SD NAND(贴片式T卡):智能皮电手环(GSR智能手环)性能与存储的深度评测
  • 基于C#实现中央定位服务器的 P2P 网络聊天系统
  • deepseek梳理java高级开发工程师算法面试题
  • windows文件共享另一台电脑资源管理器网络文件夹无法找到机器
  • 数字IC后端零基础入门基础理论(Day2)
  • 【算法】版本号排序
  • 端午假期购票日历发布,今日可购买5月29日火车票
  • 夜读丨读《汉书》一得
  • 首次采用“顶置主星+侧挂从星”布局,长二丁“1箭12星”发射成功
  • 持续8年仍难终了的纠纷:败诉方因拒执罪被立案,胜诉方银行账户遭冻结
  • 马上评丨火车穿村而过多人被撞身亡,亡羊补牢慢不得
  • 西藏日喀则市拉孜县发生5.5级地震,震感明显部分人被晃醒