java - Android Hostname was not be verified. How to allow all hosts? -
java - Android Hostname was not be verified. How to allow all hosts? -
i trying image url starts https. maintain getting hostname not verified exception.
i took @ question java.io.ioexception: hostname not verified didn't understood how create work.
is there way can allow hostnames?
here's code thats giving me trouble:
public drawable drawablefromurl(string url) { bitmap x; httpurlconnection connection; seek { connection = (httpurlconnection) new url(url).openconnection(); connection.connect(); inputstream input = connection.getinputstream(); x = bitmapfactory.decodestream(input); homecoming new bitmapdrawable(x); } grab (malformedurlexception e) { e.printstacktrace(); } grab (ioexception e) { homecoming null; } homecoming null; } thanks in advance help
this error occurs when tls certificate either self-signed or domain on certificate doesn't match server's host name.
this answer provides finish solution:
/** * disables ssl certificate checking new instances of {@link httpsurlconnection} has been created * aid testing on local box, not utilize on production. */ private static void disablesslcertificatechecking() { trustmanager[] trustallcerts = new trustmanager[] { new x509trustmanager() { @override public void checkclienttrusted(java.security.cert.x509certificate[] x509certificates, string s) throws java.security.cert.certificateexception { // not implemented } @override public void checkservertrusted(java.security.cert.x509certificate[] x509certificates, string s) throws java.security.cert.certificateexception { // not implemented } @override public java.security.cert.x509certificate[] getacceptedissuers() { homecoming null; } } }; seek { httpsurlconnection.setdefaulthostnameverifier(new hostnameverifier() { @override public boolean verify(string s, sslsession sslsession) { homecoming true; } }); sslcontext sc = sslcontext.getinstance("tls"); sc.init(null, trustallcerts, new java.security.securerandom()); httpsurlconnection.setdefaultsslsocketfactory(sc.getsocketfactory()); } grab (keymanagementexception e) { e.printstacktrace(); } grab (nosuchalgorithmexception e) { e.printstacktrace(); } } simply run 1 time @ time before making first https connection. if command server, though, preferred seek , obtain valid certificate instead.
java android httpurlconnection
Comments
Post a Comment