国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

https請求報錯unable to find valid certification path to requested target解決

這篇具有很好參考價值的文章主要介紹了https請求報錯unable to find valid certification path to requested target解決。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

? ? ? ? 在Java項目中請求HTTPS時,可能會遇到 "unable to find valid certification path to requested target" 錯誤。這個錯誤通常是由于SSL證書問題引起的。要解決此問題,可以嘗試以下方法

1.忽略SSL驗證

????????OkHttpClient封裝請求

public static OkHttpClient getUnsafeOkHttpClient() {
        try {
            // 創(chuàng)建一個信任所有證書的TrustManager
            final TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                        }

                        @Override
                        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                        }

                        @Override
                        public X509Certificate[] getAcceptedIssuers() {
                            return new X509Certificate[0];
                        }
                    }
            };

            // 創(chuàng)建一個不驗證證書的 SSLContext,并使用上面的TrustManager初始化
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

            // 使用上面創(chuàng)建的SSLContext創(chuàng)建一個SSLSocketFactory
            javax.net.ssl.SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
            builder.hostnameVerifier((hostname, session) -> true);
            builder.readTimeout(1, TimeUnit.MINUTES);

            return builder.build();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) throws Exception {
        
        // 發(fā)送請求
        Request request = new Request.Builder()
                .url("https://example.com")
                .build();

        Response response = getUnsafeOkHttpClient().newCall(request).execute();
        System.out.println(response.body().string());
    }
????????CloseableHttpClient請求
public static void main(String[] args) throws Exception {
        // 創(chuàng)建SSL上下文,忽略證書驗證
        SSLContextBuilder sslContextBuilder = SSLContexts.custom().loadTrustMaterial((chain, authType) -> true);
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(), NoopHostnameVerifier.INSTANCE);


        // 創(chuàng)建 CloseableHttpClient 對象
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(sslSocketFactory)
                .build();

        // 創(chuàng)建 HttpGet 對象,并設(shè)置請求URL
        HttpGet httpGet = new HttpGet("https://lmg.jj20.com/up/allimg/4k/s/02/2109250006343S5-0-lp.jpg");

        // 設(shè)置請求頭參數(shù)
        httpGet.setHeader("User-Agent", "Mozilla/5.0");

        // 發(fā)送請求,獲取響應
        HttpResponse response = httpClient.execute(httpGet);

        // 獲取響應實體
        HttpEntity entity = response.getEntity();

        // 讀取響應內(nèi)容
        String responseBody = EntityUtils.toString(entity);

        // 輸出響應
        System.out.println("Response Code: " + response.getStatusLine().getStatusCode());
        System.out.println("Response Body: " + responseBody);

        // 關(guān)閉httpClient
        httpClient.close();
    }

? ? ? ? HttpURLConnection請求

//忽略SSL驗證
    public static void ignoreSSL() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext sslContext = SSLContext.getInstance("TLS");
        TrustManager[] trustManagers = new TrustManager[]{new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) {}

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) {}

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        }};
        sslContext.init(null, trustManagers, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    }

    public static void main(String[] args) throws Exception {
        ignoreSSL();
        // 創(chuàng)建URL對象
        URL url = new URL("https://lmg.jj20.com/up/allimg/4k/s/02/2109250006343S5-0-lp.jpg");

        // 打開連接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 設(shè)置請求頭參數(shù)
        connection.setRequestMethod("GET");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0");

        // 發(fā)送請求
        int responseCode = connection.getResponseCode();

        // 讀取響應
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        // 輸出響應
        System.out.println("Response Code: " + responseCode);
        System.out.println("Response Body: " + response.toString());

        // 關(guān)閉連接
        connection.disconnect();
    }

????????RestTemplate請求

public static void ignoreSSL() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext sslContext = SSLContext.getInstance("TLS");
        TrustManager[] trustManagers = new TrustManager[]{new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) {}

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) {}

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        }};
        sslContext.init(null, trustManagers, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    }

    public static void main(String str[]) throws Exception{
        ignoreSSL();
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.getForEntity("https://lmg.jj20.com/up/allimg/4k/s/02/2109250006343S5-0-lp.jpg", String.class);
        System.out.println(response.getBody());
    }
2.添加證書到本地證書庫
  • 獲取證書,首先確保您訪問的HTTPS網(wǎng)站具有有效的SSL證書??梢酝ㄟ^瀏覽器訪問該網(wǎng)站并查看并導出證書。

https請求報錯unable to find valid certification path to requested target解決,https,網(wǎng)絡(luò)協(xié)議,java

?

  • 導入SSL證書:將SSL證書導入到Java的信任存儲庫中??梢允褂胟eytool命令行工具執(zhí)行此操作。運行以下命令將證書導入到默認的JDK信任存儲庫中文章來源地址http://www.zghlxwxcb.cn/news/detail-719451.html

  1. keytool -import -alias alias_name -keystore path_to_jdk_cacerts -file path_to_certificate
    
    -- alias_name 證書指定的別名
    -- path_to_jdk_cacerts是JDK信任存儲庫的路徑,默認路徑為$JAVA_HOME/jre/lib/security/cacerts,
    -- path_to_certificate是下載的SSL證書的路徑

到了這里,關(guān)于https請求報錯unable to find valid certification path to requested target解決的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • 如何解決HTTPS請求報錯sun.security.validator.ValidatorException: PKIX path building failed

    如何解決HTTPS請求報錯sun.security.validator.ValidatorException: PKIX path building failed

    在Java中進行網(wǎng)絡(luò)請求時出現(xiàn)\\\"sun.security.validator.ValidatorException: PKIX path building failed\\\"錯誤通常是由于SSL證書驗證失敗引起的。這種錯誤可能由以下幾種原因?qū)е拢?1、證書鏈不完整或證書不受信任: Java使用TrustStore來驗證SSL證書的有效性。如果服務器使用的SSL證書不在Java TrustS

    2024年04月13日
    瀏覽(86)
  • RuntimeError: Unable to find a valid cuDNN algorithm to run convolution

    RuntimeError: Unable to find a valid cuDNN algorithm to run convolution

    使用yolov5l模型訓練時出現(xiàn)報錯,但是昨天使用yolov5s模型時是可以正常訓練的。 發(fā)生報錯的原因是gpu內(nèi)存占用過高,terminal輸入nvidia-smi查看gpu的使用情況。 ? 我們需要把bach_size調(diào)小,一般建議是8的倍數(shù),內(nèi)存不夠用時盡量調(diào)低,此處我設(shè)置成了16。 結(jié)果運行正常。 使用yol

    2024年02月11日
    瀏覽(97)
  • Unable to connect to the server: x509: certificate has expired or is not yet valid

    手動更新所有證書,執(zhí)行命令 更新用戶配置 用更新后的admin.conf替換/root/.kube/config文件 k8s解決證書過期官方文檔:https://kubernetes.io/zh-cn/docs/tasks/administer-cluster/kubeadm/kubeadm-certs/ 幫助文檔: https://www.cnblogs.com/00986014w/p/13095628.html

    2024年02月04日
    瀏覽(100)
  • MINIO 配置https報錯:x509:cannot validate certificate【已解決】

    MINIO 配置https報錯:x509:cannot validate certificate【已解決】

    為MINIO配置https時,首先按官方要求官網(wǎng)描述(How to secure access to MinIO server with TLS),將TLS的公私鑰放到:{{HOME}}/.minio/certs 里。 注意: 私鑰需要命名為:private.key 公鑰需要命名為:public.crt (如果公鑰是以pem格式結(jié)尾,可直接改為crt格式) 但配置完成后會遇到如下錯誤,x509:c

    2024年02月14日
    瀏覽(92)
  • 已解決unable to access ‘https://github.com/**‘: SSL certificate problem: unable to get

    錯誤:unable to access \\\'https://github.com/\\\': SSL certificate problem: unable to get local issuer certificate 這個問題是由于沒有配置信任的服務器HTTPS驗證。默認,curl被設(shè)為不信任任何CAS,就是說,它不信任任何服務器驗證。 只需要執(zhí)行下面命令就可以解決: ?

    2024年03月19日
    瀏覽(88)
  • 【bug解決】RuntimeError: Unable to find a valid cuDNN algorithm to run convolution

    進行深度學習的算法模型訓練的時候,終端報錯: 產(chǎn)生報錯的原因可能有兩種: 1.模型訓練的環(huán)境中cudnn,CUDA的版本號不匹配 解決辦法:安裝對應的cudnn,以及cuda,找到對應的torch框架,進行安裝 2.其實問題更加簡單,是模型的訓練的batch-size訓練過大了,調(diào)整更小,就可以了

    2024年02月11日
    瀏覽(90)
  • K8S異常之Unable to connect to the server: x509: certificate has expired or is not yet valid

    K8S異常之Unable to connect to the server: x509: certificate has expired or is not yet valid

    2.1 處理步驟 2.2 處理步驟詳細情況 如上,發(fā)現(xiàn)很多證書都是 invalid 的狀態(tài),接著更新證書: 如下,更新證書后,證書過期時間已經(jīng)更新為 365d 3.1 再次查看kubectl get node,發(fā)現(xiàn)有新的錯誤: error: You must be logged in to the server (Unauthorized) 3.2 上述錯誤解決方案 備份配置文件 cp -rp

    2024年02月03日
    瀏覽(99)
  • Git Clone 報錯 `SSL certificate problem: unable to get local issuer certificate`

    Git Clone 報錯 `SSL certificate problem: unable to get local issuer certificate`

    如果您在嘗試克隆Git存儲庫時得到 “SSL certificate problem: unable to get local issuer certificate” 的錯誤,這意味著Git無法驗證遠程存儲庫的SSL證書。如果SSL證書是自簽名的,或者SSL證書鏈有問題,就會發(fā)生這種情況。 想要修復這個錯誤,可以嘗試以下解決方案: 一、 禁用SSL驗證: 一般

    2024年02月07日
    瀏覽(114)
  • 關(guān)于picgo圖床報錯“unable to verify the first certificate“

    關(guān)于picgo圖床報錯“unable to verify the first certificate“

    關(guān)于picgo圖床報錯\\\"unable to verify the first certificate\\\" 編程上的疑難雜癥(一) 問題:本人picgo加github圖床上傳出現(xiàn)以下問題 \\\"message\\\": \\\"unable to verify the first certificate\\\"(無法驗證第一證書) 問題分析: 圖床是github圖床,工具是picgo,為了可以順利訪問github用到steam++(Watt Toolkit)加速

    2024年02月11日
    瀏覽(191)
  • Composer出現(xiàn) SSL certificate problem: unable to get local issuer certificate 報錯的解決方法

    Composer出現(xiàn) SSL certificate problem: unable to get local issuer certificate 報錯的解決方法

    Composer出現(xiàn)crul SSL報錯的問題是沒有安裝CA證書導致的?。?! 錯誤信息如下: [ComposerDownloaderTransportException] ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? curl error 60 while downloading https://repo.packagist.org/package

    2024年02月03日
    瀏覽(122)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包