沈阳建设网站服务公司东营市住房和城乡建设管理局网站
2026/1/12 19:02:13 网站建设 项目流程
沈阳建设网站服务公司,东营市住房和城乡建设管理局网站,岳阳做网站哪家好,wordpress 咨询主题1. 前言#xff1a; artemis-http-client sdk 中提供获取门禁事件图片的方法#xff0c;但实际图片访问地址为该响应的重定向地址 问题来了#xff1a;虽然他提供了 sdk #xff0c;但没有办法通过 sdk 获取重定向的地址于是产生了本文#xff0c;自己通过hutools的 httpU…1. 前言artemis-http-clientsdk 中提供获取门禁事件图片的方法但实际图片访问地址为该响应的重定向地址问题来了虽然他提供了 sdk 但没有办法通过 sdk 获取重定向的地址于是产生了本文自己通过hutools的 httpUtil调用2. 签名工具类HikSignUtil 记得先引入一下 artemis-http-clientpackagecom.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_FORMATTERDateTimeFormatter.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 */publicstaticMapString,StringbuildHeaders(StringappKey,StringappSecret,Stringurl,Stringbody){// Content-MD5请求体的MD5值Base64编码StringcontentMd5Base64.encode(DigestUtil.md5(body));// Date 头RFC1123StringdateRFC_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 urlStringBuildersignBuildernewStringBuilder();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);// urlStringsignStringsignBuilder.toString();// 使用 HmacSHA256 算法计算签名HMachmacnewHMac(HmacAlgorithm.HmacSHA256,appSecret.getBytes(StandardCharsets.UTF_8));StringsignatureBase64.encode(hmac.digest(signString));// 构建请求头MapString,StringheadersnewHashMap();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. 使用示例1protectedstaticfinalArtemisConfigartemisConfignewArtemisConfig(192.XX.13.XX:443,123,123);/** * 使用自建 HttpClient 调用海康接口便于排查网络/签名问题 */publicstaticPageResponseResultEventInfoDtogetEventListWithHttpClient(EventsRequesteventsRequest)throwsException{if(eventsRequestnull){eventsRequestnewEventsRequest();}Stringurl/artemis/api/acs/v2/door/events;StringfullUrlhttps://artemisConfig.getHost()url;StringbodyJSON.toJSONString(eventsRequest);MapString,StringheadersHikSignUtil.buildHeaders(artemisConfig.getAppKey(),artemisConfig.getAppSecret(),url,body);try(CloseableHttpClienthttpClientcreateHttpClient(false)){HttpPosthttpPostnewHttpPost(fullUrl);headers.forEach(httpPost::setHeader);httpPost.setEntity(newStringEntity(body,ContentType.APPLICATION_JSON));try(CloseableHttpResponseresponsehttpClient.execute(httpPost)){intstatusresponse.getStatusLine().getStatusCode();StringrespBodyresponse.getEntity()null?null:EntityUtils.toString(response.getEntity());if(statusHttpStatus.SC_OK){returncheckResp(respBody,newTypeReferencePageResponseResultEventInfoDto(){});}thrownewException(获取事件列表失败状态码: status, 响应: respBody);}}}示例2protectedstaticfinalArtemisConfigartemisConfignewArtemisConfig(192.XX.13.XX:443,123,123);publicstaticStringpictures(StringsvrIndexCode,StringpicUri)throwsException{Stringurl/artemis/api/acs/v1/event/pictures;StringfullUrlhttps://artemisConfig.getHost()url;// 构建请求体PicturesRequestrequestnewPicturesRequest(svrIndexCode,picUri);StringbodyJSON.toJSONString(request);// 使用签名工具生成请求头MapString,StringheadersHikSignUtil.buildHeaders(artemisConfig.getAppKey(),artemisConfig.getAppSecret(),url,body);try(CloseableHttpClienthttpClientcreateHttpClient(false)){HttpPosthttpPostnewHttpPost(fullUrl);headers.forEach(httpPost::setHeader);httpPost.setEntity(newStringEntity(body,ContentType.APPLICATION_JSON));try(CloseableHttpResponseresponsehttpClient.execute(httpPost)){intstatusresponse.getStatusLine().getStatusCode();if(statusHttpStatus.SC_MOVED_TEMPORARILY||statusHttpStatus.SC_MOVED_PERMANENTLY||statusHttpStatus.SC_SEE_OTHER||statusHttpStatus.SC_TEMPORARY_REDIRECT||statusHTTP_STATUS_PERMANENT_REDIRECT){HeaderlocationHeaderresponse.getFirstHeader(Location);if(locationHeader!nullStrUtil.isNotBlank(locationHeader.getValue())){returnlocationHeader.getValue();}thrownewException(302重定向但未获取到Location头);}StringrespBodyresponse.getEntity()null?null:EntityUtils.toString(response.getEntity());thrownewException(获取图片失败状态码: status, 响应: respBody);}}}

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询