美文网首页
Android中Glide加载Https图片失败的解决方案

Android中Glide加载Https图片失败的解决方案

作者: hao_developer | 来源:发表于2021-12-22 13:58 被阅读0次

在Application中添加如下方法:(该方法有效)

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        handleSSLHandshake()
    }

    private fun handleSSLHandshake() {
        try {
            val trustAllCerts: Array<TrustManager> =
                arrayOf<TrustManager>(object : X509TrustManager {
                    val acceptedIssuers: Array<Any?>?
                        get() = arrayOfNulls(0)

                    override fun checkClientTrusted(certs: Array<X509Certificate?>?, authType: String?) {}
                    override fun checkServerTrusted(certs: Array<X509Certificate?>?, authType: String?) {}
                    override fun getAcceptedIssuers(): Array<out X509Certificate>? {
                        return null
                    }
                })
            val sc: SSLContext = SSLContext.getInstance("TLS")
            // trustAllCerts信任所有的证书
            sc.init(null, trustAllCerts, SecureRandom())
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory())
            HttpsURLConnection.setDefaultHostnameVerifier { hostname, session -> true }
        } catch (ignored: Exception) {
        }
    }

}

相关文章

网友评论

      本文标题:Android中Glide加载Https图片失败的解决方案

      本文链接:https://www.haomeiwen.com/subject/tnuqqrtx.html