ssl - android https - self signed server certificate - exception -
ssl - android https - self signed server certificate - exception -
referred links:
http://developer.android.com/training/articles/security-ssl.html#unknownca http://randomizedsort.blogspot.com/2010/09/step-to-step-guide-to-programming.html
i have next code connecting server self-signed certificate (i utilize answer keystore part) :
seek { final keystore ks = keystore.getinstance("bks"); final inputstream inputstream = getapplicationcontext().getresources().openrawresource(r.raw.certs); ks.load(inputstream, getapplicationcontext().getstring(r.string.store_pass).tochararray()); inputstream.close(); trustmanagerfactory tmf = trustmanagerfactory.getinstance(trustmanagerfactory.getdefaultalgorithm()); tmf.init(ks); sslcontext context = sslcontext.getinstance("tls"); context.init(null, tmf.gettrustmanagers(), new securerandom()); url url = new url("https://www.mywebsite.com/"); httpsurlconnection urlconnection = (httpsurlconnection)url.openconnection(); urlconnection.setsslsocketfactory(context.getsocketfactory()); inputstream in = urlconnection.getinputstream(); } catch(exception e) { toast.maketext(getapplicationcontext(), e.getmessage(), toast.length_long).show(); // toast appear empty (described in question) }
if utilize website's name :
http://www.mywebsite.com
then in surrounding seek catch, exception thrown :
com.android.okhttp.internal.http.httpurlconnectionimpl cannot cast javax.net.ssl.httpsurlconnection
if utilize :
https://www.mywebsite.com
then still exception, due reason, exception object 'e' in catch(exception e)
has empty message (its not null), can't figure out exception.
if seek utilize :
log.d("test", e.getmessage());
then get:
java.lang.nullpointerexception: println needs message
can 1 point out doing wrong ?
edit:
here's output of e.printstacktrace()
d/test﹕ e.printstacktrace() : 06-18 14:20:17.493 w/system.err﹕ android.os.networkonmainthreadexception 06-18 14:20:17.493 w/system.err﹕ @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1145) 06-18 14:20:17.493 w/system.err﹕ @ java.net.inetaddress.lookuphostbyname(inetaddress.java:385) 06-18 14:20:17.493 w/system.err﹕ @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236) 06-18 14:20:17.493 w/system.err﹕ @ java.net.inetaddress.getallbyname(inetaddress.java:214) 06-18 14:20:17.493 w/system.err﹕ @ com.android.okhttp.internal.dns$1.getallbyname(dns.java:28) 06-18 14:20:17.493 w/system.err﹕ @ com.android.okhttp.internal.http.routeselector.resetnextinetsocketaddress(routeselector.java:216) 06-18 14:20:17.493 w/system.err﹕ @ com.android.okhttp.internal.http.routeselector.next(routeselector.java:122) 06-18 14:20:17.493 w/system.err﹕ @ com.android.okhttp.internal.http.httpengine.connect(httpengine.java:292) 06-18 14:20:17.493 w/system.err﹕ @ com.android.okhttp.internal.http.httpengine.sendsocketrequest(httpengine.java:255) 06-18 14:20:17.493 w/system.err﹕ @ com.android.okhttp.internal.http.httpengine.sendrequest(httpengine.java:206) 06-18 14:20:17.493 w/system.err﹕ @ com.android.okhttp.internal.http.httpurlconnectionimpl.execute(httpurlconnectionimpl.java:345) 06-18 14:20:17.493 w/system.err﹕ @ com.android.okhttp.internal.http.httpurlconnectionimpl.getresponse(httpurlconnectionimpl.java:296) 06-18 14:20:17.493 w/system.err﹕ @ com.android.okhttp.internal.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:179) 06-18 14:20:17.493 w/system.err﹕ @ com.android.okhttp.internal.http.httpsurlconnectionimpl.getinputstream(httpsurlconnectionimpl.java:246) 06-18 14:20:17.493 w/system.err﹕ @ com.android.authmobile.app.testactivity$1.onclick(testactivity.java:90) 06-18 14:20:17.493 w/system.err﹕ @ android.view.view.performclick(view.java:4438) 06-18 14:20:17.497 w/system.err﹕ @ android.view.view$performclick.run(view.java:18422) 06-18 14:20:17.497 w/system.err﹕ @ android.os.handler.handlecallback(handler.java:733) 06-18 14:20:17.497 w/system.err﹕ @ android.os.handler.dispatchmessage(handler.java:95) 06-18 14:20:17.497 w/system.err﹕ @ android.os.looper.loop(looper.java:136) 06-18 14:20:17.497 w/system.err﹕ @ android.app.activitythread.main(activitythread.java:5017) 06-18 14:20:17.497 w/system.err﹕ @ java.lang.reflect.method.invokenative(native method) 06-18 14:20:17.497 w/system.err﹕ @ java.lang.reflect.method.invoke(method.java:515) 06-18 14:20:17.497 w/system.err﹕ @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 06-18 14:20:17.497 w/system.err﹕ @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 06-18 14:20:17.497 w/system.err﹕ @ dalvik.system.nativestart.main(native method)
you're getting networkonmainthreadexception. newer versions of android not allow perform network tasks in same thread ui, can cause app lock , go non-responsive. move network operation asynctask. see android training doc connecting network.
android ssl
Comments
Post a Comment