Skip to content

Instantly share code, notes, and snippets.

@Skeptick
Created March 10, 2021 09:39
Show Gist options
  • Save Skeptick/1122cdb783f389b10dd1f44950e4b7e4 to your computer and use it in GitHub Desktop.
Save Skeptick/1122cdb783f389b10dd1f44950e4b7e4 to your computer and use it in GitHub Desktop.
OkHttp. Trust all SSL certificates
private fun buildHttpClientEngine(): HttpClientEngine = OkHttp.create {
config {
retryOnConnectionFailure(true)
hostnameVerifier { _, _ -> true }
val trustManager = object : X509TrustManager {
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) = Unit
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) = Unit
override fun getAcceptedIssuers(): Array<X509Certificate> = emptyArray()
}
val sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, arrayOf(trustManager), SecureRandom())
sslSocketFactory(sslContext.socketFactory, trustManager)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment