accepting HTTPS connections with self-signed certificates

前端 未结 13 2096
小蘑菇
小蘑菇 2020-11-22 04:20

I\'m trying to make HTTPS connections, using HttpClient lib, but the problem is that, since the certificate isn\'t signed by a recognized Certificate Authority

13条回答
  •  心在旅途
    2020-11-22 05:04

    Jan 19th, 2020 Self Signed Certificate ISSUE FIX:

    To play video , image , calling webservice for any self signed certificate or connecting to any unsecured url just call this method before performing any action , it will fix your issue regarding certificate issue :

    KOTLIN CODE

      private fun disableSSLCertificateChecking() {
            val hostnameVerifier = object: HostnameVerifier {
                override fun verify(s:String, sslSession: SSLSession):Boolean {
                    return true
                }
            }
            val trustAllCerts = arrayOf(object: X509TrustManager {
                override fun getAcceptedIssuers(): Array {
                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                }
    
                //val acceptedIssuers:Array = null
                @Throws(CertificateException::class)
                override fun checkClientTrusted(arg0:Array, arg1:String) {// Not implemented
                }
                @Throws(CertificateException::class)
                override fun checkServerTrusted(arg0:Array, arg1:String) {// Not implemented
                }
            })
            try
            {
                val sc = SSLContext.getInstance("TLS")
                sc.init(null, trustAllCerts, java.security.SecureRandom())
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory())
                HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier)
            }
            catch (e: KeyManagementException) {
                e.printStackTrace()
            }
            catch (e: NoSuchAlgorithmException) {
                e.printStackTrace()
            }
        }
    

提交回复
热议问题