kotlin库,可以做无http证书validation的httpS连接(如curl –insecure)

我需要抓取已过期/自签名证书的公司内部网站。 没有人会为该主机配置有效的证书,所以我必须使用不安全的连接。

curl有 – 为此目的,

Scala finagle库有.tlsWithoutValidation()模式。

:是否有一个Kotlin库有类似的选择?

UPD :到目前为止我使用燃料与在这里find的javish解决方法,但仍然在寻找更好的方法..

 fun useInsecureSSL() { // Create a trust manager that does not validate certificate chains val trustAllCerts = arrayOf(object : X509TrustManager { override fun getAcceptedIssuers(): Array? = null override fun checkClientTrusted(chain: Array, authType: String) = Unit override fun checkServerTrusted(chain: Array, authType: String) = Unit }) val sc = SSLContext.getInstance("SSL") sc.init(null, trustAllCerts, java.security.SecureRandom()) HttpsURLConnection.setDefaultSSLSocketFactory(sc.socketFactory) // Create all-trusting host name verifier val allHostsValid = HostnameVerifier { _, _ -> true } // Install the all-trusting host verifier HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid) } 

上面的解决方法工作,但它是太冗长,似乎为我的应用程序所做的每个连接设置不安全模式,不仅为特定的一个。