Resolve Imageloader load HTTPS picture certificate Check exception problem

Source: Internet
Author: User

During the development of a project, it was found that using universal-image-loader could not load the HTTPS picture resource and view the log found that the certificate check failed.

View source discovery, Imageloader can be overridden by inheriting baseimagedownloader to download some of the logic, to achieve the following

1  Public classAuthimagedownloaderextendsBaseimagedownloader {2 3      Public Static FinalString TAG = Authimagedownloader.class. GetName ();4 5     /**6      * {@value}7      */8      Public Static Final intDefault_http_connect_timeout = 5 * 1000;//milliseconds9     /**Ten      * {@value} One      */ A      Public Static Final intDefault_http_read_timeout = 20 * 1000;//milliseconds -  -      PublicAuthimagedownloader (Context context) { the          This(context, default_http_connect_timeout, default_http_read_timeout); -     } -  -      PublicAuthimagedownloader (Context context,intConnectTimeout,intreadtimeout) { +         Super(context, connecttimeout, readtimeout); -     } +  A @Override at     protectedInputStream getstreamfromnetwork (String Imageuri, Object Extra)throwsIOException { -  -URL url =NULL; -         Try { -URL =NewURL (Imageuri); -}Catch(malformedurlexception e) { in log.e (TAG, E.getmessage (), e); -         } toHttpURLConnection http =NULL; +  -         if(Scheme.ofuri (imageuri) = =Scheme.https) { the trustallhosts (); *Httpsurlconnection HTTPS =(httpsurlconnection) URL $ . OpenConnection ();Panax Notoginseng Https.sethostnameverifier (do_not_verify); -HTTP =https; the Http.connect (); +}Else { AHTTP =(HttpURLConnection) url.openconnection (); the         } +  - http.setconnecttimeout (connecttimeout); $ http.setreadtimeout (readtimeout); $         return NewFlushedinputstream (NewBufferedinputstream ( - Http.getinputstream ())); -     } the  -     //Always verify the Host-dont check for certificateWuyi     Final StaticHostnameverifier do_not_verify =NewHostnameverifier () { the @Override -          Public BooleanVerify (String hostname, sslsession session) { Wu             return true; -         } About     }; $  -     /** - * Trust every server-dont check for any certificate -      */ A     Private Static voidtrustallhosts () { +         //Create A trust manager that does not validate certificate chains thetrustmanager[] Trustallcerts =Newtrustmanager[]{NewX509trustmanager () { - @Override $              Public voidcheckclienttrusted ( the java.security.cert.x509certificate[] X509Certificates, theString s)throwsjava.security.cert.CertificateException { the             } the  - @Override in              Public voidcheckservertrusted ( the java.security.cert.x509certificate[] X509Certificates, theString s)throwsjava.security.cert.CertificateException { About             } the  the @Override the              Publicjava.security.cert.x509certificate[] Getacceptedissuers () { +                 return Newjava.security.cert.x509certificate[]{}; -             } the         }};Bayi  the         //Install the All-trusting trust manager the         Try { -Sslcontext sc = sslcontext.getinstance ("TLS"); -Sc.init (NULL, Trustallcerts,Newjava.security.SecureRandom ()); the httpsurlconnection the . Setdefaultsslsocketfactory (Sc.getsocketfactory ()); the}Catch(Exception e) { the e.printstacktrace (); -         } the     } the}
View Code

Then configure the Imageloader in the

Imageloaderconfiguration.builder config = new Imageloaderconfiguration.builder (                context);.. Config.imagedownloader (New Authimagedownloader (this));

Problem solving, OK

Resolve Imageloader load HTTPS picture certificate Check exception problem

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.