Hello, I am trying to configure an OkHttpClient u...
# ktor
z
Hello, I am trying to configure an OkHttpClient using the HttpClient function for Websockets on Android. I am trying to configure an insecure client, but despite allowing all hostnames and certificates in the config block of the engine, the websocket fails to load with the SSLPeerUnverifiedException. Does anyone have an example of how to configure an insecure websocket client for Android with OkHttp?
c
can’t speak for Android, this works on JVM:
Copy code
private fun createHttpClient(): HttpClient {
        return HttpClient(OkHttp) {
            install(ContentNegotiation) { jackson() }
            install(Logging) {
                logger = Logger.DEFAULT
                level = LogLevel.ALL
            }
            engine {
                config {
                    val sslContext = SSLContext.getInstance("TLS")
                    val trustAllCerts = TrustAllX509TrustManager()
                    sslContext.init(null, arrayOf(trustAllCerts), SecureRandom())
                    sslSocketFactory(sslContext.socketFactory, trustAllCerts)

                    hostnameVerifier { _, _ -> true }
                }
            }
        }
    }
s
Not sure if this is required for Websockets, but for regular Http connections you need to enable clear text traffic (reference)
Copy code
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">your_host</domain>
    </domain-config>
</network-security-config>