Skip to content

Instantly share code, notes, and snippets.

@sczerwinski
Last active January 25, 2017 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sczerwinski/c7da8c7ce2369dc8874c9fe5f1c5dbdb to your computer and use it in GitHub Desktop.
Save sczerwinski/c7da8c7ce2369dc8874c9fe5f1c5dbdb to your computer and use it in GitHub Desktop.
Kotlin port of the solution proposed by @naderghanabri: http://stackoverflow.com/a/28787883/4568679
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.*
object SSL {
fun trustAll() {
val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, arrayOf(TrustAll), SecureRandom())
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.socketFactory)
HttpsURLConnection.setDefaultHostnameVerifier(VerifyAllHostnames)
}
private object TrustAll : X509TrustManager {
override fun getAcceptedIssuers() = null
override fun checkClientTrusted(x509Certificates: Array<X509Certificate>, s: String) {}
override fun checkServerTrusted(x509Certificates: Array<X509Certificate>, s: String) {}
}
private object VerifyAllHostnames : HostnameVerifier {
override fun verify(s: String, sslSession: SSLSession) = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment