问题:
使用自定义证书的https链接接入项目,运行项目时报错:
javax.net.ssl.SSLPeerUnverifiedException: Hostname xxx not verified
分析:
CommonName和“IP地址”,和服务器的ip地址不一致
解决:
方法1,通过重新制作证书,保证CN,IP地址同服务器IP地址一致
方法2,允许所有证书,给builder设置hostnameVerifier如下:
okHttpClient = new OkHttpClient.Builder()
.readTimeout(READ_TIME_OUT, TimeUnit.MILLISECONDS)
.connectTimeout(CONNECT_TIME_OUT, TimeUnit.MILLISECONDS)
.addInterceptor(mRewriteCacheControlInterceptor)
.addNetworkInterceptor(mRewriteCacheControlInterceptor)
.addInterceptor(headerInterceptor)
.addInterceptor(logInterceptor)
.hostnameVerifier(new AllowAllHostnameVerifier())
.cache(cache)
.build();