news 2026/5/12 3:41:32

手写海康OpenApi签名规范,实现手动调用api(sdk:artemis-http-client)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
手写海康OpenApi签名规范,实现手动调用api(sdk:artemis-http-client)

1. 前言:

artemis-http-clientsdk 中提供获取门禁事件图片的方法,但实际图片访问地址为该响应的重定向地址
问题来了:虽然他提供了 sdk ,但没有办法通过 sdk 获取重定向的地址于是产生了本文,自己通过hutools的 httpUtil调用

2. 签名工具类

HikSignUtil 记得先引入一下 artemis-http-client

packagecom.lxsy.util;importcn.hutool.core.codec.Base64;importcn.hutool.crypto.digest.DigestUtil;importcn.hutool.crypto.digest.HMac;importcn.hutool.crypto.digest.HmacAlgorithm;importjava.nio.charset.StandardCharsets;importjava.time.ZoneOffset;importjava.time.ZonedDateTime;importjava.time.format.DateTimeFormatter;importjava.util.HashMap;importjava.util.Map;/** * 海康 OpenAPI 签名工具类 * 签名算法:HmacSHA256 + Base64 */publicclassHikSignUtil{privatestaticfinalDateTimeFormatterRFC_1123_FORMATTER=DateTimeFormatter.RFC_1123_DATE_TIME.withZone(ZoneOffset.UTC);/** * 生成请求头(包含签名) * * @param appKey 应用Key * @param appSecret 应用Secret * @param url 请求的相对路径(如 /artemis/api/acs/v1/event/pictures) * @param body 请求体(JSON字符串) * @return 包含签名的请求头Map */publicstaticMap<String,String>buildHeaders(StringappKey,StringappSecret,Stringurl,Stringbody){// Content-MD5:请求体的MD5值,Base64编码StringcontentMd5=Base64.encode(DigestUtil.md5(body));// Date 头(RFC1123)Stringdate=RFC_1123_FORMATTER.format(ZonedDateTime.now(ZoneOffset.UTC));// 签名字符串拼接// httpHeaders = HTTP METHOD + "\n" + Accept + "\n" + Content-MD5 + "\n" + Content-Type + "\n" + Date + "\n"// customHeaders = "x-ca-key" + ":" + appKey + "\n"// signString = httpHeaders + customHeaders + urlStringBuildersignBuilder=newStringBuilder();signBuilder.append("POST").append("\n");// HTTP METHODsignBuilder.append("*/*").append("\n");// AcceptsignBuilder.append(contentMd5).append("\n");// Content-MD5signBuilder.append("application/json").append("\n");// Content-TypesignBuilder.append(date).append("\n");// DatesignBuilder.append("x-ca-key:").append(appKey).append("\n");// customHeaderssignBuilder.append(url);// urlStringsignString=signBuilder.toString();// 使用 HmacSHA256 算法计算签名HMachmac=newHMac(HmacAlgorithm.HmacSHA256,appSecret.getBytes(StandardCharsets.UTF_8));Stringsignature=Base64.encode(hmac.digest(signString));// 构建请求头Map<String,String>headers=newHashMap<>();headers.put("Accept","*/*");headers.put("Content-Type","application/json");headers.put("Content-MD5",contentMd5);headers.put("Date",date);headers.put("x-ca-key",appKey);headers.put("x-ca-signature",signature);headers.put("x-ca-signature-headers","x-ca-key");returnheaders;}}

3. 使用

示例1

protectedstaticfinalArtemisConfigartemisConfig=newArtemisConfig("192.XX.13.XX:443","123","123");/** * 使用自建 HttpClient 调用海康接口,便于排查网络/签名问题 */publicstaticPageResponseResult<EventInfoDto>getEventListWithHttpClient(EventsRequesteventsRequest)throwsException{if(eventsRequest==null){eventsRequest=newEventsRequest();}Stringurl="/artemis/api/acs/v2/door/events";StringfullUrl="https://"+artemisConfig.getHost()+url;Stringbody=JSON.toJSONString(eventsRequest);Map<String,String>headers=HikSignUtil.buildHeaders(artemisConfig.getAppKey(),artemisConfig.getAppSecret(),url,body);try(CloseableHttpClienthttpClient=createHttpClient(false)){HttpPosthttpPost=newHttpPost(fullUrl);headers.forEach(httpPost::setHeader);httpPost.setEntity(newStringEntity(body,ContentType.APPLICATION_JSON));try(CloseableHttpResponseresponse=httpClient.execute(httpPost)){intstatus=response.getStatusLine().getStatusCode();StringrespBody=response.getEntity()==null?null:EntityUtils.toString(response.getEntity());if(status==HttpStatus.SC_OK){returncheckResp(respBody,newTypeReference<PageResponseResult<EventInfoDto>>(){});}thrownewException("获取事件列表失败,状态码: "+status+", 响应: "+respBody);}}}

示例2

protectedstaticfinalArtemisConfigartemisConfig=newArtemisConfig("192.XX.13.XX:443","123","123");publicstaticStringpictures(StringsvrIndexCode,StringpicUri)throwsException{Stringurl="/artemis/api/acs/v1/event/pictures";StringfullUrl="https://"+artemisConfig.getHost()+url;// 构建请求体PicturesRequestrequest=newPicturesRequest(svrIndexCode,picUri);Stringbody=JSON.toJSONString(request);// 使用签名工具生成请求头Map<String,String>headers=HikSignUtil.buildHeaders(artemisConfig.getAppKey(),artemisConfig.getAppSecret(),url,body);try(CloseableHttpClienthttpClient=createHttpClient(false)){HttpPosthttpPost=newHttpPost(fullUrl);headers.forEach(httpPost::setHeader);httpPost.setEntity(newStringEntity(body,ContentType.APPLICATION_JSON));try(CloseableHttpResponseresponse=httpClient.execute(httpPost)){intstatus=response.getStatusLine().getStatusCode();if(status==HttpStatus.SC_MOVED_TEMPORARILY||status==HttpStatus.SC_MOVED_PERMANENTLY||status==HttpStatus.SC_SEE_OTHER||status==HttpStatus.SC_TEMPORARY_REDIRECT||status==HTTP_STATUS_PERMANENT_REDIRECT){HeaderlocationHeader=response.getFirstHeader("Location");if(locationHeader!=null&&StrUtil.isNotBlank(locationHeader.getValue())){returnlocationHeader.getValue();}thrownewException("302重定向但未获取到Location头");}StringrespBody=response.getEntity()==null?null:EntityUtils.toString(response.getEntity());thrownewException("获取图片失败,状态码: "+status+", 响应: "+respBody);}}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/12 22:41:29

NVIDIA TensorRT-LLM高性能推理框架解析

NVIDIA TensorRT-LLM高性能推理框架解析 在大语言模型&#xff08;LLMs&#xff09;加速落地的今天&#xff0c;一个现实问题摆在所有AI工程团队面前&#xff1a;如何让百亿参数的模型不仅“能跑”&#xff0c;还要“跑得快、成本低、稳得住”&#xff1f;PyTorch 虽然训练灵活…

作者头像 李华
网站建设 2026/5/4 8:00:47

Git补丁零基础入门:从创建到应用的完整指南

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建一个交互式Git补丁学习工具&#xff0c;包含&#xff1a;1)可视化补丁创建向导 2)实时演练环境 3)常见错误模拟与修正 4)渐进式难度练习 5)即时反馈系统。要求界面友好&#xf…

作者头像 李华
网站建设 2026/5/11 4:46:00

零基础教程:20分钟用Llama Factory训练你的第一个AI模型

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 创建新手友好型训练向导&#xff1a;1.使用电影评论数据集 2.分步指导数据上传 3.自动选择情感分析预设模板 4.简化参数配置界面 5.生成训练进度可视化看板。要求每个步骤都有示意图…

作者头像 李华
网站建设 2026/5/10 19:59:42

给文科生看的Kubernetes:用快递系统理解容器编排

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 开发一个面向初学者的Kubernetes交互式学习项目&#xff0c;包含&#xff1a;1) 用快递系统类比Pod/Node/Service的动画演示 2) 可视化命令行模拟器 3) 安全沙箱环境 4) 渐进式难度…

作者头像 李华
网站建设 2026/4/30 23:20:33

效率对比:5种Ubuntu安装Node.js方法耗时测评

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容&#xff1a; 请生成一个自动化测试脚本&#xff0c;比较以下5种Node.js安装方法在Ubuntu 22.04上的耗时&#xff1a;1.apt官方源 2.NodeSource源 3.nvm安装 4.二进制包安装 5.Docker容器。脚本应…

作者头像 李华
网站建设 2026/5/10 10:47:32

Linux线程操作全指南

Linux线程概述与操作指南线程与进程对比线程是轻量级进程&#xff0c;属于某个进程&#xff0c;共享进程资源但拥有独立栈区&#xff08;默认8MB&#xff09;。进程资源独立&#xff0c;稳定性更高&#xff1b;线程崩溃可能导致整个进程崩溃。线程创建开销更小&#xff08;仅需…

作者头像 李华