NVR800 HttpCliebt Digest鉴权()

参考链接:https://blog.csdn.net/LZHH_2008/article/details/127266312https://www.cnblogs.com/liyuanhong/p/16007750.htmlpom 文件引入 HttpClient5

        <dependency>
            <groupId>org.apache.httpcomponents.client5</groupId>
            <artifactId>httpclient5</artifactId>
            <version>5.2</version>
        </dependency>

鉴权

package com.lv.controller.yzsba;

import com.alibaba.fastjson.JSONObject;
import org.apache.hc.client5.http.ContextBuilder;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.auth.CredentialsProviderBuilder;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.StatusLine;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import static org.apache.hc.client5.http.impl.classic.HttpClients.createDefault;

public class Test {
    public static void main(String[] args) throws Exception {
        getLoginRes();
    }
    /** 解决ssl报错问题,信任所有证书 */
    public static CloseableHttpClient createSSLClientDefault() {
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                // 信任所有
                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
            HttpClientConnectionManager connMgr = PoolingHttpClientConnectionManagerBuilder.create().setSSLSocketFactory(sslFactory).build();
            return HttpClients.custom().setConnectionManager(connMgr).setUserAgent("MyService").build();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        return createDefault();

    }

    /** 获取登陆后的cookie和cookie */
    public static JSONObject getLoginRes() throws Exception {

        try (final CloseableHttpClient httpclient = createSSLClientDefault()) {

            final HttpHost target = new HttpHost("https", "192.168.0.100", 443);
            final HttpClientContext localContext = ContextBuilder.create()
                    .useCredentialsProvider(CredentialsProviderBuilder.create()
                            .add(target, new UsernamePasswordCredentials("username", "password".toCharArray()))
                            .build())
                    .build();

            final HttpPost httpPost = new HttpPost("https://192.168.0.100/API/Web/Login");

            System.out.println("Executing request " + httpPost.getMethod() + " " + httpPost.getUri());
            ClassicHttpResponse res = httpclient.execute(httpPost, localContext, response -> {
                System.out.println("----------------------------------------");
                System.out.println(httpPost + "->" + new StatusLine(response));
                EntityUtils.consume(response.getEntity());
                return response;
            });

            if (res.getCode() == 200) {
                String cookie = res.getHeader("Set-Cookie").getValue();
                System.out.println(cookie.substring(0, cookie.indexOf(";")));
                System.out.println(res.getHeader("X-csrftoken").getValue());
            } else {
                System.out.println(res.getCode() + " " + res.getReasonPhrase());
            }
        }
        return null;
    }
}
————————

参考链接:https://blog.csdn.net/LZHH_2008/article/details/127266312https://www.cnblogs.com/liyuanhong/p/16007750.htmlpom 文件引入 HttpClient5

        <dependency>
            <groupId>org.apache.httpcomponents.client5</groupId>
            <artifactId>httpclient5</artifactId>
            <version>5.2</version>
        </dependency>

鉴权

package com.lv.controller.yzsba;

import com.alibaba.fastjson.JSONObject;
import org.apache.hc.client5.http.ContextBuilder;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.auth.CredentialsProviderBuilder;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.StatusLine;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import static org.apache.hc.client5.http.impl.classic.HttpClients.createDefault;

public class Test {
    public static void main(String[] args) throws Exception {
        getLoginRes();
    }
    /** 解决ssl报错问题,信任所有证书 */
    public static CloseableHttpClient createSSLClientDefault() {
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                // 信任所有
                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
            HttpClientConnectionManager connMgr = PoolingHttpClientConnectionManagerBuilder.create().setSSLSocketFactory(sslFactory).build();
            return HttpClients.custom().setConnectionManager(connMgr).setUserAgent("MyService").build();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        return createDefault();

    }

    /** 获取登陆后的cookie和cookie */
    public static JSONObject getLoginRes() throws Exception {

        try (final CloseableHttpClient httpclient = createSSLClientDefault()) {

            final HttpHost target = new HttpHost("https", "192.168.0.100", 443);
            final HttpClientContext localContext = ContextBuilder.create()
                    .useCredentialsProvider(CredentialsProviderBuilder.create()
                            .add(target, new UsernamePasswordCredentials("username", "password".toCharArray()))
                            .build())
                    .build();

            final HttpPost httpPost = new HttpPost("https://192.168.0.100/API/Web/Login");

            System.out.println("Executing request " + httpPost.getMethod() + " " + httpPost.getUri());
            ClassicHttpResponse res = httpclient.execute(httpPost, localContext, response -> {
                System.out.println("----------------------------------------");
                System.out.println(httpPost + "->" + new StatusLine(response));
                EntityUtils.consume(response.getEntity());
                return response;
            });

            if (res.getCode() == 200) {
                String cookie = res.getHeader("Set-Cookie").getValue();
                System.out.println(cookie.substring(0, cookie.indexOf(";")));
                System.out.println(res.getHeader("X-csrftoken").getValue());
            } else {
                System.out.println(res.getCode() + " " + res.getReasonPhrase());
            }
        }
        return null;
    }
}