Android-Universal-Image-Loader icon indicating copy to clipboard operation
Android-Universal-Image-Loader copied to clipboard

Image-Loader does not support https protocol

Open youngWM opened this issue 7 years ago • 0 comments

sorry my pool English!

when url start with “https://”, it throw exception :

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

and this is my solve:

private HttpURLConnection getHttpURLConnectionBy(String url)throws Exception{
    URL url1 = new URL(url);

    if (url.startsWith("https://")){
        HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier());
        //设置SSLContext
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[]{myX509TrustManager}, null);
        HttpsURLConnection httpUrlConn4Util = (HttpsURLConnection) url1.openConnection();
        httpUrlConn4Util.setSSLSocketFactory(sslcontext.getSocketFactory());

        return httpUrlConn4Util;
    }else{
        HttpURLConnection httpUrlConn4Util = (HttpURLConnection) url1.openConnection();
        return httpUrlConn4Util;
    }
}

private static TrustManager myX509TrustManager = new X509TrustManager() {

  @Override
  public X509Certificate[] getAcceptedIssuers() {
         return null;
     }

  @Override
  public void checkServerTrusted(X509Certificate[] chain, String authType)
  throws CertificateException {
       }

   @Override
  public void checkClientTrusted(X509Certificate[] chain, String authType)
  throws CertificateException {
   }

};

private class NullHostNameVerifier implements HostnameVerifier {

    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }

}

youngWM avatar Jul 21 '17 07:07 youngWM