zhanghua
2025-07-16 c4c1c6cf89781eb06dc02b677a8954fdb2666c43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package ga;
 
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
 
import static com.hikvision.artemis.sdk.util.HttpUtil.wrapClient;
 
public class ArtemisPostTest {
    /**
     * 请根据技术支持提供的实际的平台IP/端口和API网关中的合作方信息更换static静态块中的三个参数.
     * [1 host]
     *         host格式为IP:Port,如10.0.0.1:443
     *         当使用https协议调用接口时,IP是平台(nginx)IP,Port是https协议的端口;
     *     当使用http协议调用接口时,IP是artemis服务的IP,Port是artemis服务的端口(默认9016)。
     * [2 appKey和appSecret]
     *         请按照技术支持提供的合作方Key和合作方Secret修改
     *         appKey:合作方Key
     *         appSecret:合作方Secret
     * 调用前看清接口传入的是什么,是传入json就用doPostStringArtemis方法,是表单提交就用doPostFromArtemis方法
     *
     */
    /**
     * API网关的后端服务上下文为:/artemis
     */
    private static final String ARTEMIS_PATH = "/artemis";
 
    /**
     * 调用POST请求类型接口,这里以获取组织列表为例
     * 接口实际url:https://ip:port/artemis/api/resource/v1/org/orgList
     *
     * @return
     */
    public static String callPostApiGetOrgList() throws Exception {
        /**
         * https://ip:port/artemis/api/resource/v1/org/orgList
         * 通过查阅AI Cloud开放平台文档或网关门户的文档可以看到获取组织列表的接口定义,该接口为POST请求的Rest接口, 入参为JSON字符串,接口协议为https。
         * ArtemisHttpUtil工具类提供了doPostStringArtemis调用POST请求的方法,入参可传JSON字符串, 请阅读开发指南了解方法入参,没有的参数可传null
         */
        ArtemisConfig config = new ArtemisConfig();
        config.setHost("127.0.0.1"); // 代理API网关nginx服务器ip端口
        config.setAppKey("20469790");  // 秘钥appkey
        config.setAppSecret("lofnD6DbnBllHmk5YOyx");// 秘钥appSecret
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/org/orgList";
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        paramMap.put("pageNo", "1");
        paramMap.put("pageSize", "2");
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
    }
 
 
    public static String getKHVideo(String code) throws Exception {
 
        ArtemisConfig config = new ArtemisConfig();
        config.setHost("10.53.133.82:1443"); // 代理API网关nginx服务器ip端口
        config.setAppKey("29967737");  // 秘钥appkey
        config.setAppSecret("LElVf9Ct3ykStZHCmFNZ");// 秘钥appSecret
        final String getSecurityApi = ARTEMIS_PATH + "/api/video/v1/cameras/previewURLs"; // 接口路径
//        System.out.println("请求设备号:" + code);
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getSecurityApi);
            }
        };
        Map<String, String> head = new HashMap<String, String>(2) {  //get请求的head参数
            {
                put("headpost", "sky-test");
            }
        };
        Map<String, String> query = new HashMap<String, String>(2) {  //get请求的head参数
            {
                put("domainId", "0");
            }
        };
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", code);
        jsonBody.put("streamType", 0);
        jsonBody.put("protocol", "hls");
        jsonBody.put("transmode", 0);
        jsonBody.put("expand", "transcode=1&videotype=h264");
        String body = jsonBody.toString();
        //参数根据接口实际情况设置
        HttpResponse result = ArtemisHttpUtil.doPostStringImgArtemis(config, path, body, query, null, "application/json", head);
//        System.out.println("海康完成请求");
        try {
            String strResult = EntityUtils.toString(result.getEntity());
//            System.out.println("海康返回结果:" + strResult);
//            HttpResponseResult responseResult = com.alibaba.fastjson.JSONObject.parseObject(strResult, HttpResponseResult.class);
//            if ("0".equals(responseResult.getCode())) {
//                com.alibaba.fastjson.JSONObject jsonObject = responseResult.getData();
//                String url = jsonObject.getString("url");
//                return url;
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
 
    /**
     * 调用POST请求类型接口,这里以分页获取区域列表为例
     * 接口实际url:https://ip:port/artemis/api/api/resource/v1/regions
     *
     * @return
     */
    public static String callPostApiGetRegions() throws Exception {
        /**
         * https://ip:port/artemis/api/resource/v1/regions
         * 过查阅AI Cloud开放平台文档或网关门户的文档可以看到分页获取区域列表的定义,这是一个POST请求的Rest接口, 入参为JSON字符串,接口协议为https。
         * ArtemisHttpUtil工具类提供了doPostStringArtemis调用POST请求的方法,入参可传JSON字符串, 请阅读开发指南了解方法入参,没有的参数可传null
         */
        ArtemisConfig config = new ArtemisConfig();
        config.setHost("127.0.0.1"); // 代理API网关nginx服务器ip端口
        config.setAppKey("20469790");  // 秘钥appkey
        config.setAppSecret("lofnD6DbnBllHmk5YOyx");// 秘钥appSecret
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/regions";
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        paramMap.put("pageNo", "1");
        paramMap.put("pageSize", "2");
        paramMap.put("treeCode", "0");
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(config, path, body, null, null, "application/json");
    }
 
    /**
     * 调用POST接口,返回图片
     * 接口实际url:https://ip:port/artemis/api/visitor/v1/record/pictures
     *
     * @return
     */
    public static String callPostImgs() throws Exception {
        ArtemisConfig config = new ArtemisConfig();
        config.setHost("127.0.0.1"); // 代理API网关nginx服务器ip端口
        config.setAppKey("20469790");  // 秘钥appkey
        config.setAppSecret("lofnD6DbnBllHmk5YOyx");// 秘钥appSecret
        final String getSecurityApi = "/artemis" + "/api/visitor/v1/record/pictures"; // 接口路径
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getSecurityApi);
            }
        };
        Map<String, String> head = new HashMap<String, String>(2) {  //get请求的head参数
            {
                put("headpost", "sky-test");
            }
        };
        Map<String, String> query = new HashMap<String, String>(2) {  //get请求的head参数
            {
                put("domainId", "0");
            }
        };
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("svrIndexCode", "9ff58bc2-65a5-464b-b28c-daea67ba9569");
        jsonBody.put("picUri", "/pic?9dda12i40-e*5b84626c4105m5ep=t=i3p*i=d1s*i=d3b*i1d3b*855925cea-96008b--2718943z855s=5i76=");
        String body = jsonBody.toJSONString();
        //参数根据接口实际情况设置
        HttpResponse result = ArtemisHttpUtil.doPostStringImgArtemis(config, path, body, query, null, "application/json", head);
        try {
            HttpResponse resp = result;
            if (302 == resp.getStatusLine().getStatusCode()) {
                /*
                获取图片数据保存到本地
                注:1.对于有时效的图片,必须尽快保存到本地
                   2.若无时效,则可以直接保存location,后续自行访问获取
                 */
                Header header = resp.getFirstHeader("location");
                String newUrl = header.getValue();
                HttpGet httpget = new HttpGet(newUrl);
                HttpClient httpClient = wrapClient(httpget.getURI().getScheme() + "://" + httpget.getURI().getHost());
                HttpResponse execute = httpClient.execute(httpget);
                HttpEntity entity = execute.getEntity();
                InputStream in = entity.getContent();
                com.hikvision.ga.Tools.savePicToDisk(in, "d:/", "test311.jpg");
            } else {
                System.out.println("下载出错");
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return getSecurityApi;
    }
 
    public static void main(String[] args) throws Exception {
        String result = getKHVideo("33112351001320000255");
        System.out.println(result);
//        String VechicleDataResult = callPostApiGetRegions();
//        System.out.println(VechicleDataResult);
    }
 
}