javax.net.ssl.SSLHandshakeException: Connection closed by peer at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)

前端 未结 3 1419
天涯浪人
天涯浪人 2021-02-02 15:27

before api level 24 my code is working fine but it is giving me error on api level 24( 7.0 Nougat). I am not getting what\'s going wrong with my code.

First Approach i

3条回答
  •  你的背包
    2021-02-02 16:09

    Do you have used Okhttp library ? It is very good library for network calls. You can handle this exception it that also.

    I had similar issue and i have managed it with this :

    public static OkHttpClient getHttpClientForFile() {
            ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                    .tlsVersions(TlsVersion.TLS_1_0)
                    .cipherSuites(
                            CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                            CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                            CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
                            CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
                            CipherSuite.TLS_RSA_WITH_AES_128_CBC_SHA,
                            CipherSuite.TLS_RSA_WITH_3DES_EDE_CBC_SHA)
                    .build();
            return new OkHttpClient.Builder()
                    .connectTimeout(2, TimeUnit.MINUTES)
                    .writeTimeout(2, TimeUnit.MINUTES)
                    .readTimeout(3, TimeUnit.MINUTES)
                    .connectionSpecs(Collections.singletonList(spec))
                    .protocols(Arrays.asList(Protocol.HTTP_1_1))
                    .build();
        }
    

    I don't know whether it is good or not, but it is working for me.

    The class you have used SSLSocketFactory may produce error after publishing app on play store or play store may give you warning to change your code.

    You can find Okhttp library from https://github.com/square/okhttp.

提交回复
热议问题