首页 > 解决方案 > D/OkHttp: <-- HTTP FAILED: javax.net.ssl.SSLException: SSL handshake aborted: ssl=0x64e3c938: I/O error during system call, Connection reset by peer

问题描述

D/OkHttp: <-- HTTP FAILED: javax.net.ssl.SSLException: SSL handshake aborted: ssl=0x64e3c938: I/O error during system call, Connection reset by peer

我在 Android 4.2.2 设备上收到此错误。在其他设备上运行相同的应用程序时,它运行良好。请帮忙。

public static Retrofit getClient(final Context context, final Server newServer) {
    if(retrofit == null || server == null || !getServer().equals(newServer) || tok != null) {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder okHttpClient = new OkHttpClient()
            .newBuilder().addInterceptor(loggingInterceptor);

        okHttpClient.addInterceptor( new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                  String hh=tok.replace("\"", "");  //For removing the " from the token
                    Request newRequest = chain
                            .request()
                            .newBuilder()
                            .addHeader(HTTP_AUTH_HEADER,"Bearer " + hh) //token use for the Authentication.
                            .build();
                    return chain.proceed(newRequest);
                }
            });


        retrofit = new Retrofit.Builder()
                .baseUrl(getBaseUrl(context, server))
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient.build())
                .build();
    }
    return retrofit;
}
private static String getBaseUrl(Context context, Server newServer) {
    StringBuilder builder = new StringBuilder();
    server = newServer; // update server address
    if(server != null && server.getAddress() != null) {
        return builder.append(server.getAddress()).toString();
    } else { // set default address
        return builder.append(BuildConfig.SERVER_ADDRESS).toString();
    }
}
}

标签: javaandroidsslretrofitretrofit2

解决方案


Android 4.4 及更低版本的设备没有默认TLSv1.2支持。因此,您需要手动制作。

先写下下面的方法。

    public static OkHttpClient.Builder enableTls12OnPreLollipop(OkHttpClient.Builder client) {
    if (Build.VERSION.SDK_INT < 22) {
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            client.sslSocketFactory(new Tls12SocketFactory(sc.getSocketFactory()), (X509TrustManager) trustAllCerts[0]);

            ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                    .tlsVersions(TlsVersion.TLS_1_2)
                    .build();

            ConnectionSpec csslv3 = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                    .tlsVersions(TlsVersion.SSL_3_0)
                    .build();

            List<ConnectionSpec> specs = new ArrayList<>();
            specs.add(cs);
            specs.add(csslv3);
            specs.add(ConnectionSpec.COMPATIBLE_TLS);
            specs.add(ConnectionSpec.CLEARTEXT);

            client.connectionSpecs(specs);
        } catch (Exception exc) {
            Log.e("OkHttpTLSCompat", "Error while setting TLS 1.2", exc);
        }
    }
    return client;
}

现在在这一行之后OkHttpClient.Builder okHttpClient = new OkHttpClient().newBuilder().addInterceptor(loggingInterceptor);添加以下代码。

okHttpClient = enableTls12OnPreLollipop(okHttpClient);

我希望它能解决你的问题。


推荐阅读