当前位置:   article > 正文

java 调用海康摄像头digest请求头认证_digestauth 方式调用接口

digestauth 方式调用接口

技术博客 http://idea.coderyj.com/
java 调用海康摄像头digest请求头认证

1.依赖
<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>
  • 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
2.配置文件
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";
}
  • 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

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;
    }
}

  • 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
4.测试
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);
    }
}

  • 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
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/186427
推荐阅读
相关标签
  

闽ICP备14008679号