okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

okhttp3 javax.net.ssl.SSLException: Read error: ssl=0xda64a748: I/O error during system call, Connection reset by peer

Open NayaneshGupte opened this issue 7 years ago • 2 comments

I have already gone through related issues on github and SO but most of the explanations doesn't match with what I'm facing.

okhttp version : 4.2.2 MinSdkVersion : 21

Request Url : https://www.ajio.com/rilfnlwebservices/v2/rilfnl/newcategoryselection?pageId=firstpagemobileapp&uiel=Mobile

This works through volley, browser, postman etc. but not through okhttp.

My Code :

@Module internal object CommonNetworkModule {

@Singleton
@Provides
@JvmStatic
fun providesOkHttp(): OkHttpClient {

    val builder = OkHttpClient.Builder()

    builder.sslSocketFactory(getSSLSocketFactory()!!, getX509TrustManager())
            .addInterceptor(HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY })
    
    return builder.build()
}


private fun getSSLSocketFactory(): SSLSocketFactory? {

    val sc: SSLContext?
    var sslSocketFactory: SSLSocketFactory? = null
    try {
        sc = SSLContext.getInstance(TlsVersion.TLS_1_2.javaName)
        if (sc != null) {
            sc.init(null, getAllTrustManagers(), SecureRandom())
            sslSocketFactory = Tls12SocketFactory(sc.socketFactory)
        } else {
            Timber.e("SSLContext is null")
        }
    } catch (e: NoSuchAlgorithmException) {
        Timber.e(e)
    } catch (e: KeyManagementException) {
        Timber.e(e)
    }

    return sslSocketFactory
}


private fun getAllTrustManagers(): Array<TrustManager> {
    return arrayOf(getX509TrustManager())
}


private fun getX509TrustManager(): X509TrustManager {
    return object : X509TrustManager {
        override fun getAcceptedIssuers(): Array<X509Certificate> {
            return arrayOf()
        }

        @SuppressLint("TrustAllX509TrustManager")
        override fun checkClientTrusted(certs: Array<X509Certificate>, authType: String) {
        }

        @SuppressLint("TrustAllX509TrustManager")
        override fun checkServerTrusted(certs: Array<X509Certificate>, authType: String) {
        }
    }
}

} `

Error Logs :

javax.net.ssl.SSLException: Read error: ssl=0xda64a748: I/O error during system call, Connection reset by peer at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method) at com.android.org.conscrypt.NativeSsl.read(NativeSsl.java:399) at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:546) at okio.InputStreamSource.read(Okio.kt:102) at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:159) at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:349) at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:222) at okhttp3.internal.http1.Http1ExchangeCodec.readHeaderLine(Http1ExchangeCodec.kt:210) at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:181) at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:105) at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:82) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:37) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87) at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.kt:215) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184) at okhttp3.RealCall.execute(RealCall.kt:66) at retrofit2.OkHttpCall.execute(OkHttpCall.java:188) at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:45) at io.reactivex.Observable.subscribe(Observable.java:12267) at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35) at io.reactivex.Single.subscribe(Single.java:3603) at io.reactivex.internal.operators.single.SingleMap.subscribeActual(SingleMap.java:34) at io.reactivex.Single.subscribe(Single.java:3603) at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89) at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578) at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66) at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764)

NayaneshGupte avatar Nov 13 '19 07:11 NayaneshGupte

Please ask usage questions on StackOverflow next time.

Your trust manager doesn't trust your server because it doesn't trust anything.

        override fun getAcceptedIssuers(): Array<X509Certificate> {
            return arrayOf()
        }

swankjesse avatar Nov 13 '19 12:11 swankjesse

@swankjesse have added question on SO. I have added java snippet with non empty trust certificate. Still trying to find answer - https://stackoverflow.com/questions/58803642/okhttp3-hostnameverifier-caused-by-javax-net-ssl-sslexception-read-error-ssl

NayaneshGupte avatar Dec 23 '19 14:12 NayaneshGupte