Skip to content

Instantly share code, notes, and snippets.

@maiconhellmann
Last active August 26, 2024 13:06
Show Gist options
  • Save maiconhellmann/c61a533eca6d41880fd2b3f8459c07f7 to your computer and use it in GitHub Desktop.
Save maiconhellmann/c61a533eca6d41880fd2b3f8459c07f7 to your computer and use it in GitHub Desktop.
UnsafeHttpClient wrote in Kotlin
import android.annotation.SuppressLint
import okhttp3.OkHttpClient
import java.security.cert.CertificateException
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
object UnsafeOkHttpClient {
fun Builder(): OkHttpClient.Builder {
try {
// Create a trust manager that does not validate certificate chains
val trustAllCerts = arrayOf<TrustManager>(@SuppressLint("CustomX509TrustManager")
object : X509TrustManager {
@SuppressLint("TrustAllX509TrustManager")
@Throws(CertificateException::class)
override fun checkClientTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) {
// N / A
}
@SuppressLint("TrustAllX509TrustManager")
@Throws(CertificateException::class)
override fun checkServerTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) {
// N / A
}
override fun getAcceptedIssuers(): Array<java.security.cert.X509Certificate> {
return arrayOf()
}
})
// Install the all-trusting trust manager
val sslContext = SSLContext.getInstance("SSL")
sslContext.init(null, trustAllCerts, java.security.SecureRandom())
// Create an ssl socket factory with our all-trusting manager
val sslSocketFactory = sslContext.socketFactory
val builder = OkHttpClient.Builder()
builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
// builder.hostnameVerifier { _, _ -> true }
builder.hostnameVerifier(hostnameVerifier = { _, _ -> true })
return builder
} catch (e: Exception) {
throw RuntimeException(e)
}
}
}
@kotleni
Copy link

kotleni commented Dec 17, 2021

thx

@Sums-beep
Copy link

How to call this to my main activity?

@minhdangoz
Copy link

good job!

@vishavgoria
Copy link

Missing import: import javax.net.ssl.HostnameVerifier

@RavinCristiano
Copy link

for me still not working. Showing same error again and again. Please help me out.

@maiconhellmann
Copy link
Author

maiconhellmann commented Aug 11, 2023

Missing import: import javax.net.ssl.HostnameVerifier

@vishavgoria added, thanks for letting me know.

@RavinCristiano May you share some logs? It's been a while since a last used this approach, I'm not sure it still works.

@maiconhellmann
Copy link
Author

If anyone is wondering out there, it still works!
I've just updated it with suppress warnings and updated syntax.

@rafinhaa
Copy link

how to use in react native?

@maiconhellmann
Copy link
Author

how to use in react native?

You have to create a module/plugin(not sure how it's called) containing it.
I'd like to emphasize that it's just for testing purposes, do not use it on production. If your Https is not verified it can potentially open up doors for suspicious things. Be careful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment