httpclient海康ISAPI透传

可以使用海康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);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值