赞
踩
技术博客 http://idea.coderyj.com/
java 调用海康摄像头digest请求头认证
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.1</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.28</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>2.0.39</version> </dependency>
import lombok.Data; import java.io.File; import java.util.HashMap; import java.util.Map; @Data public class FormRequestConfig { /** * 协议,http或者https */ private String agreement = "http"; /** * example 175.13.254.22 */ private String ip; /** * 端口 */ private Integer port = 80; /** * 账号 * example admin */ private String username; /** * 原密码 * example 123456 */ private String password; /** * 除了协议、ip,端口后面的uri * example /ISAPI/System/TwowayAudio/channels */ private String uri; /** * 请求参数 */ private Map<String,String> formData = new HashMap<>(); private File file; /** * 文件请求的name */ private String fileName; /** * application/json * text/xml */ private String type = "application/json"; }
3.http工具类
import org.apache.http.*; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.*; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.conn.ConnectionKeepAliveStrategy; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.StringBody; 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.message.BasicHeaderElementIterator; import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; import java.io.FileNotFoundException; public class HkHttpUtils { /** * get请求 * @param config * @return * @throws Exception */ public String doGet(BodyRequestConfig config) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort()); UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword()); credsProvider.setCredentials(favourite_digest_realm,usernamePasswordCredentials); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); HttpGet httpGet = new HttpGet(config.getAgreement()+"://"+config.getIp()+":"+config.getPort()+config.getUri()); CloseableHttpResponse responseBody = httpclient.execute(httpGet); HttpEntity responseEntity = responseBody.getEntity(); if (responseEntity != null) { String response = EntityUtils.toString(responseEntity); return response; } return null; } /** * delete请求 * @param config * @return * @throws Exception */ public String doDelete(BodyRequestConfig config) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort()); UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword()); credsProvider.setCredentials(favourite_digest_realm,usernamePasswordCredentials); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); HttpDelete httpDelete = new HttpDelete(config.getAgreement()+"://"+config.getIp()+":"+config.getPort()+config.getUri()); CloseableHttpResponse responseBody = httpclient.execute(httpDelete); HttpEntity responseEntity = responseBody.getEntity(); if (responseEntity != null) { String response = EntityUtils.toString(responseEntity); return response; } return null; } /** * body方式发起post请求 * @param config * @return * @throws Exception */ public String doBodyPost(BodyRequestConfig config) throws Exception { String entity = config.getEntity(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort()); UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword()); credsProvider.setCredentials(favourite_digest_realm,usernamePasswordCredentials); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); HttpPost httpPost = new HttpPost(config.getAgreement()+"://"+config.getIp()+":"+config.getPort()+config.getUri()); httpPost.setEntity(new StringEntity(entity, "UTF-8")); CloseableHttpResponse responseBody = httpclient.execute(httpPost); HttpEntity responseEntity = responseBody.getEntity(); if (responseEntity != null) { String response = EntityUtils.toString(responseEntity); return response; } return null; } /** * body方式发起post请求 * @param config * @return * @throws Exception */ public String doBodyPut(BodyRequestConfig config) throws Exception { String entity = config.getEntity(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort()); UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword()); credsProvider.setCredentials(favourite_digest_realm,usernamePasswordCredentials); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); HttpPut HttpPut = new HttpPut(config.getAgreement()+"://"+config.getIp()+":"+config.getPort()+config.getUri()); HttpPut.setEntity(new StringEntity(entity, "UTF-8")); CloseableHttpResponse responseBody = httpclient.execute(HttpPut); HttpEntity responseEntity = responseBody.getEntity(); if (responseEntity != null) { String response = EntityUtils.toString(responseEntity); return response; } return null; } /** * 以表单方式发起 post 请求 * @param config * @return * @throws Exception */ public String doFormDataPost(FormRequestConfig config) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort()); UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword()); credsProvider.setCredentials(favourite_digest_realm,usernamePasswordCredentials); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); HttpPost httpPost = new HttpPost(config.getAgreement()+"://"+config.getIp()+":"+config.getPort()+config.getUri()); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); config.getFormData().forEach((k,v)->{ builder.addPart(k,new StringBody(v, ContentType.create(config.getType(), Consts.UTF_8))); }); if (config.getFile()!=null){ if (!config.getFile().exists()){ throw new FileNotFoundException("文件不存在!"+config.getFile().getAbsolutePath()); } builder.addBinaryBody(config.getFileName(),config.getFile(),ContentType.create("image/jpeg"),config.getFile().getName()); } HttpEntity reqEntity = builder.build(); httpPost.setEntity(reqEntity); CloseableHttpResponse responseBody = httpclient.execute(httpPost); HttpEntity responseEntity = responseBody.getEntity(); if (responseEntity != null) { String response = EntityUtils.toString(responseEntity); return response; } return null; } /** * 表单方式发起 put 请求 * @param config * @return * @throws Exception */ public String doFormDataPut(FormRequestConfig config) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope favourite_digest_realm = new AuthScope(config.getIp(), config.getPort()); UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(config.getUsername(), config.getPassword()); credsProvider.setCredentials(favourite_digest_realm,usernamePasswordCredentials); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); HttpPut httpPut = new HttpPut(config.getAgreement()+"://"+config.getIp()+":"+config.getPort()+config.getUri()); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); config.getFormData().forEach((k,v)->{ builder.addPart(k,new StringBody(v, ContentType.create(config.getType(), Consts.UTF_8))); }); if (config.getFile()!=null){ if (!config.getFile().exists()){ throw new FileNotFoundException("文件不存在!"+config.getFile().getAbsolutePath()); } builder.addBinaryBody(config.getFileName(),config.getFile(),ContentType.create("image/jpeg"),config.getFile().getName()); } HttpEntity reqEntity = builder.build(); httpPut.setEntity(reqEntity); CloseableHttpResponse responseBody = httpclient.execute(httpPut); HttpEntity responseEntity = responseBody.getEntity(); if (responseEntity != null) { String response = EntityUtils.toString(responseEntity); return response; } return null; } }
import com.alibaba.fastjson.JSONObject; import javax.sound.midi.Soundbank; import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; public class HKTestService { HkHttpUtils hik = new HkHttpUtils(); public static void main(String[] args) throws Exception { HKTestService test = new HKTestService(); // /ISAPI/system/TwowayAudio/channels/capabilities // /ISAPI/System/TwowayAudio/channels String url = "/ISAPI/System/TwowayAudio/channels"; test.doGet(url); } public void doFormDataPut() throws Exception { FormRequestConfig config = new FormRequestConfig(); config.setIp(""); config.setUsername("admin"); config.setPassword("12345"); config.setUri("/ISAPI/Intelligent/FDLib/FDSetUp?format=json"); config.setFileName("img"); config.setFile(new File("C:\\Users\\faceImgs\\01.jpg")); config.getFormData().put("FaceDataRecord","{\"faceLibType\":\"blackFD\",\"FDID\":\"1\",\"FPID\":\"qiang\"}"); String put = hik.doFormDataPut(config); System.out.println(put); } public void doFormDataPost() throws Exception { FormRequestConfig config = new FormRequestConfig(); config.setIp(""); config.setUsername("admin"); config.setPassword("12345"); config.setUri("/ISAPI/Intelligent/FDLib/1/picture?type=concurrent"); config.setFileName("importImage"); config.setFile(new File("C:\\Users\\Desktop\\测试图片及文档\\faceImgs\\01.jpg")); config.getFormData().put("FaceAppendData","<?xml version='1.0' encoding='UTF-8'?><FaceAppendData><name>001</name></FaceAppendData>"); String post = hik.doFormDataPost(config); System.out.println(post); } public void doBodyPost(String[] args) throws Exception{ BodyRequestConfig config = new BodyRequestConfig(); config.setIp(""); config.setUsername("admin"); config.setPassword("12345"); config.setUri("/ISAPI/AccessControl/UserInfo/Search?format=json"); config.setEntity("{\n" + " \"UserInfoSearchCond\":{\n" + " \"searchID\":\"20200706 19:17:03\",\n" + " \"searchResultPosition\":0,\n" + " \"maxResults\":30\n" + " }\n" + "}"); String post = hik.doBodyPost(config); System.out.println(post); } public void doBodyPut(String[] args) throws Exception{ BodyRequestConfig config = new BodyRequestConfig(); config.setIp("172.16.1.233"); config.setUsername("admin"); config.setPassword("hik12345"); config.setUri("/ISAPI/AccessControl/UserInfo/Delete?format=json"); config.setEntity("{\n" + " \"UserInfoDelCond\": {\n" + " \"EmployeeNoList\": [\n" + " {\n" + " \"employeeNo\": \"111\"\n" + " }\n" + " ]\n" + " }\n" + "}"); String put = hik.doBodyPut(config); System.out.println(put); } public void doGet(String url) throws Exception{ BodyRequestConfig config = new BodyRequestConfig(); config.setIp("172.16.1.233"); config.setUsername("admin"); config.setPassword("abcd1234"); // /ISAPI/system/TwowayAudio/channels/capabilities config.setUri(url); String get = hik.doGet(config); System.out.println("得到xml结果" + get); JSONObject jsonObject = CommonUtils.xmlToJSON(get); System.out.println(jsonObject); } public void doDelete(String[] args) throws Exception{ BodyRequestConfig config = new BodyRequestConfig(); config.setIp(""); config.setUsername("admin"); config.setPassword("12345"); config.setUri("/ISAPI/Intelligent/FDLib/1/picture/5"); String delete = hik.doDelete(config); System.out.println(delete); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。