Java 请求https接口不需要安装证书


/**
* SSL请求HTTPS 不需要证书,用获取到的Client发起请求即可

         * org.apache.http.impl.client.CloseableHttpClient
*/
public static CloseableHttpClient createSSLClientDefault(){
try {
             SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                //信任所有
                @Override
public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType)
throws java.security.cert.CertificateException {
return true;
}
             }).build();
             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
             return HttpClients.custom().setSSLSocketFactory(sslsf).build();
         } catch (KeyManagementException e) {
        LOGGER.error("createSSLClientDefault KeyManagementException error",e);
         } catch (NoSuchAlgorithmException e) {
        LOGGER.error("createSSLClientDefault NoSuchAlgorithmException error",e);
         } catch (KeyStoreException e) {
        LOGGER.error("createSSLClientDefault KeyStoreException error",e);
         }
         return  HttpClients.createDefault();
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaHTTPS 协议的过程中,需要使用 SSLContext 和 HttpsURLConnection 类。SSLContext 类用于创建 SSL 连接,HttpsURLConnection 用于发送 HTTPS 请求。 下面是一个简单的示例代码: ```java import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class HttpsClient { public static void main(String[] args) throws Exception { // 创建 SSL 连接 SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[] { new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } } }, null); // 打开 HTTPS 连接 URL url = new URL("https://example.com/api"); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setSSLSocketFactory(sslContext.getSocketFactory()); // 发送 HTTPS 请求 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } } ``` 在上面的示例代码中,我们创建了一个 SSLContext 对象,使用 TrustManager 来实现信任所有证书。然后,我们打开 HTTPS 连接,并将 SSLContext 对象的 SocketFactory 设置为连接的 SSLSocketFactory。最后,我们发送 HTTPS 请求,并读取响应内容。 需要注意的是,为了避免信任所有证书带来的安全风险,实际使用中应该使用自己的证书或信任指定的证书
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值