可以使用海康SDK调用NET_DVR_STDXMLConfig进行透传,但是这种方式仍然比较麻烦。SDK的透传其实就是http的包装,可以完全撇开海康SDK,也就是通过http的方式获取或者设置,例如系统设置-基本信息
可以使用postman进行测试,注意鉴权方式选择Digest Auth。
直接上java代码
package com.cscec.hik.common.util;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import cn.hutool.json.JSONObject;
import cn.hutool.json.XML;
public class ISAPIUtil {
public static void main(String[] args) throws Exception {
String ip = "摄像头ip";
String username = "摄像头用户名";
String password = "摄像头密码";
String ftpISAPIUrl = "/ISAPI/System/deviceInfo";
JSONObject res = getISAPI(ip, username, password, ftpISAPIUrl);
System.out.println(res);
String putXml = "<?xml version: \"1.0\" encoding=\"UTF-8\"?><DeviceInfo xmlns=\"http://www.hikvision.com/ver20/XMLSchema\" version=\"2.0\">"
+ "<deviceName>摄像头1</deviceName>" + "<telecontrolID>88</telecontrolID>" + "</DeviceInfo>";
res = putISAPI(ip, username, password, ftpISAPIUrl, putXml);
System.out.println(res);
res = getISAPI(ip, username, password, ftpISAPIUrl);
System.out.println(res);
}
public static JSONObject getISAPI(String ip, String username, String password, String isapiUrl) throws Exception {
String url = "http://" + ip + isapiUrl;
HttpGet httpGet = new HttpGet(url);
Credentials creds = new UsernamePasswordCredentials(username, password);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,creds);
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
HttpResponse response = httpclient.execute(httpGet);
String resultString = EntityUtils.toString(response.getEntity(), "utf-8");
return XML.toJSONObject(resultString);
}
public static JSONObject putISAPI(String ip, String username, String password, String isapiUrl, String putXml)
throws Exception {
String url = "http://" + ip + isapiUrl;
HttpPut httpPut = new HttpPut(url);
Credentials creds = new UsernamePasswordCredentials(username, password);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,creds);
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
httpPut.setEntity(new StringEntity(putXml, "UTF-8"));
HttpResponse response = httpclient.execute(httpPut);
String resultString = EntityUtils.toString(response.getEntity(), "utf-8");
return XML.toJSONObject(resultString);
}
}