背景:调用第三方接口,使用的是digest auth鉴权方式,
basic auth和digest auth比较:
basic认证是把用户和密码通过base64加密后发送给服务器进行验证。
Basic认证过程简单,每次请求都有发送密码。安全性较低。
为了解决Basic模式安全问题,HTTP1.1时提出了Digest模式,它的基本原理是把服务器响应的401消息里面的特定的值和用户名以及密码结合起来进行不可逆的摘要算法运算得到一个值,然后把用户名和这个摘要值发给服务器,服务通过用户名去 在自己本地找到对应的密码,然后进行同样的摘要运算,再比较这个值是否和客户端发过来的摘要值一样。
具体详见此网页描述
1.先上实体类 BodyRequestConfig 、FormRequestConfig
/**
 * 适用于 参数放  请求体的提交可以是JOSN或者XML
 */
public class BodyRequestConfig {
    /**
     * 协议,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/AccessControl/UserInfo/Record?format=json
     */
    private String uri;
    /**
     * 请求体 JSON | XML
     */
    private String entity;
    public String getAgreement() {
        return agreement;
    }
    public void setAgreement(String agreement) {
        this.agreement = agreement;
    }
    public String getIp() {
        return ip;
    }
    public void setIp(String ip) {
        this.ip = ip;
    }
    public Integer getPort() {
        return port;
    }
    public void setPort(Integer port) {
        this.port = port;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getUri() {
        return uri;
    }
    public void setUri(String uri) {
        this.uri = uri;
    }
    public String getEntity() {
        return entity;
    }
    public void setEntity(String entity) {
        this.entity = entity;
    }
}import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
 * 适用于form-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/AccessControl/UserInfo/Record?format=json
     */
    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";
    public String getAgreement() {
        return agreement;
    }
    public void setAgreement(String agreement) {
        this.agreement = agreement;
    }
    public String getIp() {
        return ip;
    }
    public void setIp(String ip) {
        this.ip = ip;
    }
    public Integer getPort() {
        return port;
    }
    public void setPort(Integer port) {
        this.port = port;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getUri() {
        return uri;
    }
    public void setUri(String uri) {
        this.uri = uri;
    }
    public Map<String, String> getFormData() {
        return formData;
    }
    public File getFile() {
        return file;
    }
    public void setFile(File file) {
        this.file = file;
    }
    public void setFormData(Map<String, String> formData) {
        this.formData = formData;
    }
    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
}工具类: HikHttpUtil
import com.hik.entity.BodyRequestConfig;
import com.hik.entity.FormRequestConfig;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
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.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.util.EntityUtils;
import java.io.FileNotFoundException;
public class HikHttpUtil {
    /**
     * 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;
    }
}pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hik</groupId>
    <artifactId>ISAPIUtil</artifactId>
    <version>1.0</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <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>
    </dependencies>
</project>测试案例:
import com.hik.HikHttpUtil;
import com.hik.entity.BodyRequestConfig;
import com.hik.entity.FormRequestConfig;
import java.io.File;
public class Test {
    HikHttpUtil hik = new HikHttpUtil();
    public static void main(String[] args) throws Exception {
        Test test = new Test();
        test.doFormDataPut();
    }
    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("10.17.35.41");
        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[] args) throws Exception{
        BodyRequestConfig config = new BodyRequestConfig();
        config.setIp("");
        config.setUsername("admin");
        config.setPassword("12345");
        config.setUri("/ISAPI/Intelligent/FDLib/manualModeling?range=unmodeled&FDID=1");
        String get = hik.doGet(config);
        System.out.println(get);
    }
    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);
    }
}postMan请求第三方接口示例:

  
![[附源码]JAVA毕业设计衡师社团管理系统(系统+LW)](https://img-blog.csdnimg.cn/fbd924337d3e4b6bbbe68012bc231464.png)




![[附源码]JAVA毕业设计衡水特产展销系统(系统+LW)](https://img-blog.csdnimg.cn/4f51d9e7a1354178bfdd93af1e0eb15b.png)













