欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java中調(diào)用https請(qǐng)求忽略ssl證書認(rèn)證代碼示例

 更新時(shí)間:2024年10月26日 09:07:33   作者:新時(shí)代農(nóng)民~  
在網(wǎng)絡(luò)請(qǐng)求中經(jīng)常會(huì)遇到需要忽略證書認(rèn)證的情況,這篇文章主要介紹了java中調(diào)用https請(qǐng)求忽略ssl證書認(rèn)證的相關(guān)資料,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下

1、獲取httpclient,忽略證書認(rèn)證

public static CloseableHttpClient createSSLClientDefault() {
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                // 信任所有證書
                public boolean isTrusted(X509Certificate[] arg0, String arg1)
                        throws CertificateException {
                    return true;
                }
            }).build();
            // 創(chuàng)建主機(jī)名驗(yàn)證器,用于繞過主機(jī)名驗(yàn)證
            HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
            // 創(chuàng)建 SSL 連接套接字工廠,將自定義的 SSL 上下文和主機(jī)名驗(yàn)證器應(yīng)用于 HTTPS 連接
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
            // 創(chuàng)建自定義的 CloseableHttpClient 實(shí)例,將 SSL 連接套接字工廠應(yīng)用于 HTTP 客戶端
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        return HttpClients.createDefault();
    }

2、使用put請(qǐng)求調(diào)用

   public static String put(String url, Map<String, String> header, String param) throws Exception {
        String result = "";
        StringEntity entity = new StringEntity(param, "utf-8");
        CloseableHttpClient httpClient = null;
        try {
            httpClient = createSSLClientDefault();
            HttpPut httpPut = new HttpPut(url);
            if (MapUtils.isNotEmpty(header)) {
                for (Map.Entry<String, String> entry : header.entrySet()) {
                    httpPut.addHeader(entry.getKey(), entry.getValue());
                }
            }
            if (entity != null) {
                entity.setContentType("application/json; charset=utf-8");
                httpPut.setEntity(entity);
            }
            LogUtil.info("開始請(qǐng)求https接口:" + url );
            HttpResponse httpResponse = httpClient.execute(httpPut);
            LogUtil.info("put請(qǐng)求返回httpResponse結(jié)果:" + httpResponse);
            HttpEntity resEntity = httpResponse.getEntity();
            result = EntityUtils.toString(resEntity, "UTF-8");
            LogUtil.info("put請(qǐng)求返回結(jié)果:" + result);
        } catch (Exception e) {
            throw e;
        } finally {
            if (httpClient != null) {
                httpClient.close();
            }
        }
        return result;
    }

總結(jié) 

到此這篇關(guān)于java中調(diào)用https請(qǐng)求忽略ssl證書認(rèn)證的文章就介紹到這了,更多相關(guān)java調(diào)用https請(qǐng)求忽略ssl認(rèn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論