java - Random javax.net.ssl.SSLExceptions using OkHttp client -


in android app using okhttp client trusts ssl certificates. problem is, facing random sslexceptions. example 8 out of 10 calls fail due sslexceptions , 2 succeed.

any pointers on why might happening? please let me know if need more info.

stack trace:

javax.net.ssl.sslexception: connection closed peer         @ com.android.org.conscrypt.nativecrypto.ssl_do_handshake(native method)         @ com.android.org.conscrypt.opensslsocketimpl.starthandshake(opensslsocketimpl.java:405)         @ com.squareup.okhttp.internal.http.socketconnector.connecttls(socketconnector.java:103)         @ com.squareup.okhttp.connection.connect(connection.java:143)         @ com.squareup.okhttp.connection.connectandsetowner(connection.java:185)         @ com.squareup.okhttp.okhttpclient$1.connectandsetowner(okhttpclient.java:128)         @ com.squareup.okhttp.internal.http.httpengine.nextconnection(httpengine.java:341)         @ com.squareup.okhttp.internal.http.httpengine.connect(httpengine.java:330)         @ com.squareup.okhttp.internal.http.httpengine.sendrequest(httpengine.java:248)         @ com.squareup.okhttp.call.getresponse(call.java:273)         @ com.squareup.okhttp.call$applicationinterceptorchain.proceed(call.java:230)         @ com.squareup.okhttp.call.getresponsewithinterceptorchain(call.java:201)         @ com.squareup.okhttp.call.execute(call.java:81)         @ retrofit.client.okclient.execute(okclient.java:53)         @ retrofit.restadapter$resthandler.invokerequest(restadapter.java:326)         @ retrofit.restadapter$resthandler.access$100(restadapter.java:220)         @ retrofit.restadapter$resthandler$2.obtainresponse(restadapter.java:278)         @ retrofit.callbackrunnable.run(callbackrunnable.java:42)         @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112)         @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587)         @ retrofit.platform$android$2$1.run(platform.java:142)         @ java.lang.thread.run(thread.java:841) 

this how create okhttp client:

private okhttpclient getunsafeokhttpclient() {     try {         // create trust manager not validate certificate chains         final trustmanager[] trustallcerts = new trustmanager[]{                 new x509trustmanager() {                     @override                     public void checkclienttrusted(java.security.cert.x509certificate[] chain, string authtype) throws certificateexception {                     }                      @override                     public void checkservertrusted(java.security.cert.x509certificate[] chain, string authtype) throws certificateexception {                     }                      @override                     public java.security.cert.x509certificate[] getacceptedissuers() {                         return null;                     }                 }         };          // install all-trusting trust manager         final sslcontext sslcontext = sslcontext.getinstance("ssl");         sslcontext.init(null, trustallcerts, new java.security.securerandom());         // create ssl socket factory our all-trusting manager         final sslsocketfactory sslsocketfactory = sslcontext.getsocketfactory();           return createokhttpclientwithtimeout(sslsocketfactory);     } catch (exception e) {         throw new runtimeexception(e);     } }  protected okhttpclient createokhttpclientwithtimeout(sslsocketfactory sslsocketfactory) {     okhttpclient okhttpclient = new okhttpclient();     okhttpclient.setconnecttimeout(connect_timeout_millis, timeunit.milliseconds);     okhttpclient.setreadtimeout(read_timeout_millis, timeunit.milliseconds);     okhttpclient.setsslsocketfactory(sslsocketfactory);     okhttpclient.sethostnameverifier(new hostnameverifier() {         @override         public boolean verify(string hostname, sslsession session) {             return true;         }     });     return okhttpclient; } 

note: know trusting ssl certificates bad.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -