码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2018-01-23 20:24:58      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:ext   static   ack   trace   throws   hostname   edr   rust   on()   

大概介绍: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

标签:ext   static   ack   trace   throws   hostname   edr   rust   on()   

原文地址:https://www.cnblogs.com/xiaotieblog/p/8337288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!