javax.net.ssl.SSLPeerUnverifiedException 从 Skyscanner 检索会话密钥时出错 API - Android

javax.net.ssl.SSLPeerUnverifiedException Error in retrieving Session key from Skyscanner API - Android

我一直在使用 Sky 扫描仪 API 来检索 android 中的航班票价和其他详细信息。

我正在使用 Retrofit2 进行网络调用。问题是在检索会话密钥时出错

错误

javax.net.ssl.SSLPeerUnverifiedException: Hostname partners.api.skyscanner.net not verified:certificate: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=DN:CN=api.skyscanner.net,OU=Infrastructure,O=Skyscanner Ltd,L=London,ST=London,C=GB

API客户端

    public static Retrofit getRetrofit() {
    if (retrofit2 == null) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
                return hv.verify("partners.api.skyscanner.net", session);
            }
        }).build();

        retrofit2 = new Retrofit.Builder()
                .baseUrl(SKY_BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit2;
}

android

中 SSLPeerUnverifiedException 的引用 SO 链接

我在 BNK 评论

的帮助下解决了这个问题
  1. 在OkhttpClient对象中添加HostnameVerifier()
  2. hv.verify(hostname, session);
  3. 中添加之前检查主机名

固定代码供参考

 if (retrofit2 == null) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
                return hv.verify("api.skyscanner.net", session);
            }
        }).build();

        retrofit2 = new Retrofit.Builder()
                .baseUrl(SKY_BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }