zoukankan      html  css  js  c++  java
  • Java中https请求报错,信任所有ssl证书。解决javax.net.ssl.SSLHandshakeException: PKIX path building failed

    Java 信任所有SSL证书,HTTPS请求抛错,忽略证书请求完美解决

    • 解决方案:在openConnection之前调用该util
    • util代码

    package com.warren.util;
    
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
    
    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSession;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    
    public class SslUtils {
    
        public static void trustAllHttpsCertificates() throws Exception {
            TrustManager[] trustAllCerts = new TrustManager[1];
            TrustManager tm = new miTM();
            trustAllCerts[0] = tm;
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, null);
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        }
    
        static class miTM implements TrustManager,X509TrustManager {
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
    
            public boolean isServerTrusted(X509Certificate[] certs) {
                return true;
            }
    
            public boolean isClientTrusted(X509Certificate[] certs) {
                return true;
            }
    
            public void checkServerTrusted(X509Certificate[] certs, String authType)
                    throws CertificateException {
                return;
            }
    
            public void checkClientTrusted(X509Certificate[] certs, String authType)
                    throws CertificateException {
                return;
            }
        }
    
        /**
         * 忽略HTTPS请求的SSL证书,必须在openConnection之前调用
         * @throws Exception
         */
        public static void ignoreSsl() throws Exception{
            HostnameVerifier hv = new HostnameVerifier() {
                public boolean verify(String urlHostName, SSLSession session) {
                    return true;
                }
            };
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
        }
    }
    • 使用方式:
    //1. 下载地址
            URL url = new URL("https://p1.music.126.net/qsRYc_3XeaXUWR1vGnm6vA==/109951164743134727.jpg?param=140y140");
            //https协议的链接会因为证书问题报错
            //在openConnection前调用该方法,信任所有SSL证书
            SslUtils.ignoreSsl();
            //2. 连接到这个资源
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    
            InputStream inputStream = urlConnection.getInputStream();

    OK!

  • 相关阅读:
    (转载)Java读取ipa中info.plist获取版本信息
    (转)使用Eclipse开发Android源码
    IOS端的摇一摇功能
    (转) 自动编译iOS工程,生成app及ipa文件的方法
    Mac OS 上设置 JAVA_HOME
    macjava
    JS超级酷的导航菜单代码
    JavaScript+Css实现的鼠标悬停时动态翻滚的紫色菜单导航
    全国的省市县联动代码
    【荐】JavaScript制作可伸缩的淡绿色菜单导航
  • 原文地址:https://www.cnblogs.com/BetterWKJ/p/12620426.html
Copyright © 2011-2022 走看看