一些安卓设备上的握手失败问题

3

在某些设备上,我使用retrofit框架与服务器通信没有问题。我还使用了多个Android版本来验证代码运行情况。但是在某些设备上(三星S8),每次都会出现一个错误:“握手失败”。有人有任何想法这个问题出现的原因吗?谢谢!

以下是我的代码:

protected static Retrofit getInstanceWithoutToken() {
    final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            final Request original = chain.request();
            final Request request = original.newBuilder()
                    .method(original.method(), original.body())
                    .build();
            return chain.proceed(request);
        }
    });
    return new Retrofit.Builder()
            .baseUrl(CommonConstantsRest.REST_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient.build())
            .build();
}

也许客户端和服务器无法就要使用的协议和密码套件达成一致。您是否尝试添加日志拦截器以查看是否可以获取有关问题的更多详细信息? - Michael
@Michael谢谢你的回答。不幸的是,我只收到了相同的消息:D/OkHttp: <-- HTTP FAILED: javax.net.ssl.SSLHandshakeException: Handshake failed,而且只在特定设备上出现(这种类型的几个设备都有这个问题,所以我不认为问题出在设备上)。 - Tobias Lukoschek
如果您可以在计算机上设置虚拟 WiFi 热点,并通过该热点连接手机到互联网,那么您可以使用 Wireshark 在计算机上记录 TLS 握手过程。 - Michael
这个问题发生在特定版本的安卓系统上吗? - xiaomi
我现在也遇到了同样的问题,对于一些三星设备来说,无论是什么操作系统,甚至是Android 10,有任何消息吗? - finalpets
显示剩余2条评论
1个回答

1
问题出在SSL通信上。添加以下CipherSuite解决了问题。
protected static Retrofit getInstanceWithoutToken() {
    ConnectionSpec spec = new
            ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
            .tlsVersions(TlsVersion.TLS_1_2)
            .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)
            .build();
    final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
    httpClient.connectionSpecs(Collections.singletonList(spec));
    httpClient
            .addInterceptor(logging)
            .addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            final Request original = chain.request();
            final Request request = original.newBuilder()
                    .method(original.method(), original.body())
                    .build();
            return chain.proceed(request);
        }
    });
    return new Retrofit.Builder()
            .baseUrl(CommonConstantsRest.REST_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(httpClient.build())
            .build();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接