The URL interface at the beginning of HTTPS could not fetch data and error: PKIX Path building failed

Source: Internet
Author: User
Tags php language

About: PHP language written interface, through get or post to get the data inside. But it's a URL that starts with HTTPS.

Reason: HTTPS URL to the customer's certificate

Resolution: Skip the Certificate validation section

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  ;//This class masks the HTTPS connection requires authentication of the encryption certificate public class Gettextbyurl {public static void main (string[] args) {hostnameverifier HV = new Hostnameverifier () {public boolean verify (String Urlhostname, sslsession session) {Syst                Em.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[] Trustallc        ERTs = 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 ja        Va.security.cert.x509certificate[] Getacceptedissuers () {return null; } public boolean isservertrusted (java.security.cert.x509certificate[] certs) {return TR        Ue } public boolean isclienttrusted (java.security.cert.x509certificate[] certs) {return TR        Ue                } 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; }    }}

  

The URL interface at the beginning of HTTPS could not fetch data and error: PKIX Path building failed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.