Created
May 8, 2023 13:20
-
-
Save SerggioC/249cd64a11fc58cf75c14379a16d36bd to your computer and use it in GitHub Desktop.
UnsafeOkHttpClient trustAllCerts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun getUnsafeOkHttpClientBuilder(): OkHttpClient.Builder { | |
try { | |
val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager { | |
override fun checkClientTrusted( | |
chain: Array<java.security.cert.X509Certificate>, | |
authType: String | |
) { | |
} | |
override fun checkServerTrusted( | |
chain: Array<java.security.cert.X509Certificate>, | |
authType: String | |
) { | |
} | |
override fun getAcceptedIssuers(): Array<java.security.cert.X509Certificate> { | |
return arrayOf() | |
} | |
}) | |
val sslContext = SSLContext.getInstance("SSL") | |
sslContext.init(null, trustAllCerts, java.security.SecureRandom()) | |
val sslSocketFactory = sslContext.socketFactory | |
//sslSocketFactory.defaultCipherSuites | |
val builder = OkHttpClient.Builder() | |
builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager) | |
builder.hostnameVerifier { _, _ -> true } | |
return builder | |
} catch (e: Exception) { | |
throw RuntimeException(e) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment