https开头的URL接口无法获取数据并报错: PKIX path building failed

Posted 张小铁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了https开头的URL接口无法获取数据并报错: PKIX path building failed相关的知识,希望对你有一定的参考价值。

大概介绍:php语言写的接口,通过get或post方式获取里面的数据。但是是以https开头的URL

原因:https网址向客户的要证书

解决方案:跳过证书验证部分

package com.scheduler;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
//该类屏蔽了https连接需要加密证书的验证
public class GetTextByUrl {

    public static void main(String[] args) {
        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                System.out.println("Warning: URL Host: " + urlHostName + " vs. "
                        + session.getPeerHost());
                return true;
            }
        };
        StringBuffer msg = new StringBuffer();
        try {
            URL url = new URL("https://www.ddsdd.php");
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            URLConnection connection = url.openConnection();
            connection.connect();
            BufferedReader in = new BufferedReader((new InputStreamReader(connection.getInputStream())));
            String line;

            while ((line = in.readLine()) != null) {
                msg.append(line);
            }
            String msgString = msg.toString();
            String fannlyMsg = msgString.substring(msgString.indexOf("!") + 1);
            System.out.println(msgString);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void trustAllHttpsCertificates() throws Exception {
        javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
        javax.net.ssl.TrustManager tm = new miTM();
        trustAllCerts[0] = tm;
        javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
                .getInstance("SSL");
        sc.init(null, trustAllCerts, null);
        javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
                .getSocketFactory());
    }

    static class miTM implements javax.net.ssl.TrustManager,
            javax.net.ssl.X509TrustManager {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public boolean isServerTrusted(
                java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public boolean isClientTrusted(
                java.security.cert.X509Certificate[] certs) {
            return true;
        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }

        public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs, String authType)
                throws java.security.cert.CertificateException {
            return;
        }
    }

}

  

以上是关于https开头的URL接口无法获取数据并报错: PKIX path building failed的主要内容,如果未能解决你的问题,请参考以下文章

EasyNVR新版本无法对接到EasyNVS平台并报错login error,该如何解决?

selenium打开浏览器闪退并报错

EasyCVR启动失败并报错LibEasySnap.dll:The specified module could not be found

一个content-type引发的问题

HTTPS接口获取数据报错的问题

Debug记录:vCenter6.5突然不能访问并报错“503 Service Unavailable”