Javax.net.ssl.SSLHandshakeException:sun.security.validator problem solving, environment-related __.net

Source: Internet
Author: User

When you use HttpClient to access HTTPS resources, there is an exception, which is related to the environment, and some machines request normal.

Error Details:

Javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX Path Building failed:  
        Sun.security.provider.certpath.SunCertPathBuilderException:unable to find valid certification path to requested target At Com.sun.net.ssl.internal.ssl.Alerts.getSSLException (alerts.java:174) at Com.sun.net.ssl.internal.ssl . Sslsocketimpl.fatal (sslsocketimpl.java:1623) at Com.sun.net.ssl.internal.ssl.Handshaker.fatalSE (Handshaker.java: 198) at Com.sun.net.ssl.internal.ssl.Handshaker.fatalSE (handshaker.java:192) at Com.sun.net.ssl.intern Al.ssl.ClientHandshaker.serverCertificate (clienthandshaker.java:1074) at Com.sun.net.ssl.internal.ssl.ClientHand Shaker.processmessage (clienthandshaker.java:128) at Com.sun.net.ssl.internal.ssl.Handshaker.processLoop (Handshak er.java:529) at Com.sun.net.ssl.internal.ssl.Handshaker.process_record (handshaker.java:465) at Com.sun . Net.ssl.internal.ssl.SSLSocketImpl.reaDrecord (sslsocketimpl.java:884) at Com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake (Sslsocketi mpl.java:1120) at Com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake (sslsocketimpl.java:1147) A T Com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake (sslsocketimpl.java:1131) at Sun.net.www.protocol.htt Ps. Httpsclient.afterconnect (httpsclient.java:434) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection. Connect (abstractdelegatehttpsurlconnection.java:166) at Sun.net.www.protocol.http.HttpURLConnection.getInputStre AM (httpurlconnection.java:1049) at Sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream (httpsurlconn ectionimpl.java:234) at Com.msd.payCenter.msdPay.yeePay.util.HttpUtils.URLGet (httputils.java:67) at Co M.msd.paycenter.msdpay.yeepay.service.impl.yeepayserviceimpl.yeeautocheckorder (YeePayServiceImpl.java:298) at Co M.msd.paycenter.msdpaY.autocheckorderserviceimpl.autocheckorder (autocheckorderserviceimpl.java:83) at Sun.reflect.NativeMethodAccesso   Rimpl.invoke0 (Native method)

Workaround:

Add one of your own HTTPS protocol classes Mysslsocketfactory.java


Import java.io.IOException;  
Import java.net.InetAddress;  
Import java.net.InetSocketAddress;  
Import Java.net.Socket;  
Import java.net.SocketAddress;  
Import java.net.UnknownHostException;  
Import java.security.KeyManagementException;  
Import java.security.NoSuchAlgorithmException;  
Import java.security.cert.CertificateException;  
  
Import Java.security.cert.X509Certificate;  
Import Javax.net.SocketFactory;  
Import Javax.net.ssl.SSLContext;  
Import Javax.net.ssl.TrustManager;  
  
Import Javax.net.ssl.X509TrustManager;  
Import org.apache.commons.httpclient.ConnectTimeoutException;  
Import Org.apache.commons.httpclient.params.HttpConnectionParams;  
  
Import Org.apache.commons.httpclient.protocol.ProtocolSocketFactory; public class Mysslsocketfactory implements Protocolsocketfactory {static {System.out.println (">>&G  
    T;>in mysslsocketfactory>> ");  
  
    Private Sslcontext sslcontext = null; Private Sslcontext CreatesslconText () {Sslcontext sslcontext = null;  
            try {sslcontext = sslcontext.getinstance ("SSL"); Sslcontext.init (NULL, new trustmanager[] {new Trustanytrustmanager ()}, New J  
        Ava.security.SecureRandom ());  
        catch (NoSuchAlgorithmException e) {e.printstacktrace ();  
        catch (Keymanagementexception e) {e.printstacktrace ();  
    return sslcontext; Private Sslcontext Getsslcontext () {if (This.sslcontext = null) {This.sslcontext = c  
        Reatesslcontext ();  
    return this.sslcontext; Public socket Createsocket (socket socket, String host, int port, Boolean autoClose) throws Ioexce  
                Ption, Unknownhostexception {return Getsslcontext (). Getsocketfactory (). Createsocket (socket, host,  
    Port, AutoClose); Public Socket createsOcket (String host, int port) throws IOException, Unknownhostexception {return Getsslcontext (). Get  
    Socketfactory (). Createsocket (host, Port); Public Socket Createsocket (String host, int port, inetaddress clienthost, int clientport) throws  
                IOException, Unknownhostexception {return Getsslcontext (). Getsocketfactory (). Createsocket (host, Port,  
    ClientHost, ClientPort); Public Socket Createsocket (String host, int port, inetaddress localaddress, int localport, HTTPCO Nnectionparams params) throws IOException, Unknownhostexception, connecttimeoutexception {if (par  
        AMS = null) {throw new IllegalArgumentException ("Parameters May is not null");  
        int timeout = params.getconnectiontimeout ();  
        Socketfactory socketfactory = Getsslcontext (). Getsocketfactory (); if (timeout = = 0) {return socketfactory. Createsocket (host, Port, localaddress, LocalPort);  
            else {Socket socket = Socketfactory.createsocket ();  
            SocketAddress localaddr = new Inetsocketaddress (localaddress, LocalPort);  
            SocketAddress remoteaddr = new Inetsocketaddress (host, Port);  
            Socket.bind (LOCALADDR);  
            Socket.connect (remoteaddr, timeout);  
        return socket; } private static Class Trustanytrustmanager implements X509trustmanager {public void CHECKC  
  
        Lienttrusted (x509certificate[] chain, String authtype) throws Certificateexception {} public void checkservertrusted (x509certificate[] chain, String authtype) throws Certificateexcepti on {} public x509certificate[] Getacceptedissuers () {return new x509certificate[] {  
        };   }  
    }  
}

Request class:

Import java.io.IOException;  
  
Import Java.io.InputStream;  
Import org.apache.commons.httpclient.HttpClient;  
Import org.apache.commons.httpclient.HttpException;  
Import Org.apache.commons.httpclient.HttpMethod;  
Import Org.apache.commons.httpclient.methods.PostMethod;  
  
Import Org.apache.commons.httpclient.protocol.Protocol; 
 /** * @author Nassir Wen * @data 2012-3-31 pm 05:09:13 * @version V2.5 * @Company: MSD. * @Copyright Copyright (c)/public class Httpsrequest {/** * * @param URL * @retur n */public static string post (string url) {//Add the following two lines of code Protocol MYHTTPS = new Protocol ("H  
        TTPs ", New Mysslsocketfactory (), 443);  
          
        Protocol.registerprotocol ("https", Myhttps);  
        HttpClient client = new HttpClient ();  
        HttpMethod post = new Postmethod (URL);  
            try {Client.executemethod (POST); byte[] Responsebody = Post.getresponsebody (); 
            string result = new String (responsebody, "GBK");  
        return result;  
        catch (HttpException e) {e.printstacktrace ();  
        catch (IOException e) {e.printstacktrace ();  
        finally {post.releaseconnection ();  
    return null;   }  
}

Test:

public static void Main (string[] args) {  
        System.out.println (httpsrequest.post ("https://www.alipay.com")); Pay treasure to do test  
    }  




Httpclient.zip Download http://dl.iteye.com/topics/download/e3dfa3aa-7083-3ac9-b6c4-b544ae59a11f


Original address:http://nassir.iteye.com/blog/1112713

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.