【发布时间】:2017-05-29 18:58:49
【问题描述】:
我正在尝试使用 Retrofit 连接到 android 上的 https 服务器。这是我的OkHttpClient
@Provides
public OkHttpClient provideContactClient(){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(CipherSuite.TLS_RSA_WITH_DES_CBC_SHA,
CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
.build();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
SSLSocketFactory sslSocketFactory = null;
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null);
sslSocketFactory = sslContext.getSocketFactory();
}catch (GeneralSecurityException e){
e.printStackTrace();
}
return new OkHttpClient.Builder()
.addInterceptor(interceptor)
.connectionSpecs(Collections.singletonList(spec))
.sslSocketFactory(sslSocketFactory)
.authenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
if(responseCount(response) >= 5){
return null;
}
String credential = Credentials.basic("user", "pass");
return response.request().newBuilder().header("Authorization", credential).build();
}
})
.build();
}
但是我不断收到CLEARTEXT communication not supported: 异常
在调试RealConnection 类时,我注意到route.address() 成员没有sslSocketFactory,尽管它已在Bulider 中分配。
【问题讨论】:
-
您显然是在请求
httpURL,而不是https。或者,httpsURL 可能正在重定向到http。 -
好的,现在我在更改为 https 后得到“未找到证书路径的信任锚”。
-
现在,摆脱你的
SSLSocketFactory。 -
弹出同样的错误