D/OkHttp: <-- HTTP FAILED: javax.net.ssl.SSLException: SSL 握手中止: ssl=0x64e3c938: 系统调用期间 I/O 错误,连接由对等方重置 [英] D/OkHttp: <-- HTTP FAILED: javax.net.ssl.SSLException: SSL handshake aborted: ssl=0x64e3c938: I/O error during system call, Connection reset by peer

查看:386
本文介绍了D/OkHttp: <-- HTTP FAILED: javax.net.ssl.SSLException: SSL 握手中止: ssl=0x64e3c938: 系统调用期间 I/O 错误,连接由对等方重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

D/OkHttp: <-- HTTP FAILED: javax.net.ssl.SSLException: SSL 握手中止: ssl=0x64e3c938: 系统调用期间 I/O 错误,连接被对等重置

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 设备上收到此错误.在其他设备上运行相同的应用程序时,它工作正常.请帮忙.

I'm getting this error on an Android 4.2.2 device. While running the same application on other devices, it works fine. Please Help.

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();
    }
}
}

推荐答案

Android 4.4 及更低版本的设备没有默认的 TLSv1.2 支持.所以,你需要手动制作.

Android 4.4 and Lower devices don't have default TLSv1.2 support. So, you need to make it manually.

先写下下面的方法.

    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); 添加以下代码.

Now after this line OkHttpClient.Builder okHttpClient = new OkHttpClient().newBuilder().addInterceptor(loggingInterceptor); add the following code.

okHttpClient = enableTls12OnPreLollipop(okHttpClient);

希望它能解决您的问题.

I hope it will solve your problem.

这篇关于D/OkHttp: &lt;-- HTTP FAILED: javax.net.ssl.SSLException: SSL 握手中止: ssl=0x64e3c938: 系统调用期间 I/O 错误,连接由对等方重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆