1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| public void setHTTPS() throws IOException, KeyManagementException{
String host = URL;
URL myurl = new URL(host);
String trustStoreFileName = PATHKEYSTORE;
String trustStorePassword = PASSKEYSTORE;
try{
TrustManager[] trustManagers = createTrustManagers(trustStoreFileName, trustStorePassword);
SSLContext sslContext=SSLContext.getInstance("TLS");
sslContext.init(null,trustManagers,null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
//Chargement de la page...
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
con.connect();
}catch(Exception e){
Log.e(TAG, "ErreurTrust: " + e);
}
}
private static TrustManager[] createTrustManagers(String trustStoreFileName, String trustStorePassword) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
//create Inputstream to truststore file
java.io.InputStream inputStream = new java.io.FileInputStream(trustStoreFileName);
//create keystore object, load it with truststorefile data
KeyStore trustStore = KeyStore.getInstance("BKS");
trustStore.load(inputStream, trustStorePassword == null ? null : trustStorePassword.toCharArray());
//create trustmanager factory and load the keystore object in it
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
//return
return trustManagerFactory.getTrustManagers();
} |