When you use HTTPSurlConnection to view the HTTPS web page content and the other party is invalid, EXCEPTION is era, how to do. 1. There is a TrustManager type import com.sun.net.ssl.sslcontext; import com.sun.net.ssl.trustManager; import com.sun.Net.ssl.x509trustManager; import com.sun.net.ssl.trustManagerFactory; Public class mytribrustManager Implements x509trustManager {private keystore keystore; private string keystorepath; private char [] keystorepassword;
public MyTrustManager () {} // MyTrustManager constructor. Save off keyStore object along with // the path to the keystore (keyStorePath) and it's password // (keyStorePassword). public MyTrustManager (KeyStore keyStore, String keyStorePath, char [] keyStorePassword) {This.keystore = keystore; this.keystorepath = keystorePath; this.keystorepassword = keystorepassword;}
// isClientTrusted checks to see if the chain is in the keyStore object // This is done with a call to isChainTrusted public boolean isClientTrusted (X509Certificate [] chain).. {Return isChainTrusted (chain);}
// isServerTrusted checks to see if the chain is in the keyStore object. // This is done with a call to isChainTrusted. If not it queries the // user to see if the chain should be trusted and stored into the // keyStore object THE KeyStore Is The Saved in The File Whose Path // KeyStorePath Public Boolean Isservertrusted (X509CERTIFICATE "} {return true;}
. // getAcceptedIssuers retrieves all of the certificates in the keyStore // and returns them in an X509Certificate array public X509Certificate [] getAcceptedIssuers () {X509Certificate [] X509Certs = null; try {// See how many certificates are in the keystore int. numberOfEntry = keyStore.size (); // If there are any certificates in the keystore if (numberOfEntry> 0) {// Create an array of X509Certificates X509Certs = new X509Certificate [numberOfEntry];. // Get all of the certificate alias out Enumeration aliases = keystore.aliases ();
// Retrieve all of the certificates out of the keystore // via the alias name int i = 0;.. While (aliases.hasMoreElements ()) {X509Certs [i] = (X509Certificate) keyStore getCertificate ((String) aliases.nextElement ()); I ;}
}}} Catch (exception e) {system.out.println ("getacceptedissuers exception); x509certs = null;} Return X509CERTS;
// isChainTrusted searches the keyStore for any certificate in the // certificate chain private boolean isChainTrusted (X509Certificate [] chain) {return true;}..} 2 TrustManager register your class X509TrustManager xtm = new MyTrustManager (); TrustManager mytm [] = {xtm}; SSLContext ctx = SSLContext.getInstance ( "SSL"); ctx.init (null, mytm, null); SSLSocketFactory factory = ctx.getSocketFactory (); // Register TrustManager class (factory) HttpsURLConnection huc = (HttpsURLConnection (New URL ("http://www.aaa.com") .openConnection (); //huc.sethostnameverifier(new com.smartghost.ssl.myhostnameverifier ()); huc.setsslsocketFactory (Factory); ... ... // Error no longer