Keep getting this error recently. But my server is...
# ktor
j
Keep getting this error recently. But my server is up and running đŸ€·đŸŸ What's up?
REQUEST https://....... failed with exception: io.ktor.client.features.HttpRequestTimeoutException: Request timeout has been expired [url=https://......., request_timeout=15000 ms]
p
does your server receive this request?
j
Nope
p
it can be almost anything 🙂 some helper questions: ‱ give it worked some time ago, what changed along the way in your client code? ‱ do other HTTP requests still work, or it's a wide problem affecting all requests made by ktor client? ‱ did you try making this request with another client? does it work?
j
When I switch from data to wifi it works
But my data is working fine in other apps
p
do you try to fetch/send lots of data? maybe it just hits the timeout as it says?
j
Nope. Its just a basic request
p
what URL are you calling?
j
Its a private url for my backend
p
is there a chance that your WiFi is configured so that it can access the backend, and GSM connection cannot?
I would then expect some HTTP 403, but timeouts can happen in case of some misconfiguration
j
Copy code
single<HttpClient> {
    HttpClient(OkHttp){
        install(JsonFeature) {
            serializer = KotlinxSerializer(json)
        }

        engine {
            preconfigured = getUnsafeOkHttpClient()
        }

        install(HttpTimeout) {
            requestTimeoutMillis = 15000L
            connectTimeoutMillis = 15000L
            socketTimeoutMillis = 15000L
        }

        install(Logging) {
            logger = object : Logger {
                override fun log(message: String) {
                    Log.v("Logger Ktor =>", message)
                }

            }
            level = LogLevel.ALL
        }

        install(ResponseObserver) {
            onResponse { response ->
                Log.d("HTTP status:", "${response.status.value}")
            }
        }

        install(DefaultRequest) {
            header("Connection", "close")
        }

        expectSuccess = false
    }
}


private fun getUnsafeOkHttpClient(): OkHttpClient {
    try {
    // Create a trust manager that does not validate certificate chains
    val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
        override fun getAcceptedIssuers(): Array<X509Certificate?> = arrayOfNulls(0)

        override fun checkClientTrusted(certs: Array<X509Certificate?>?, authType: String?) {}

        override fun checkServerTrusted(certs: Array<X509Certificate?>?, authType: String?) {}
    })

    // 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

    return OkHttpClient.Builder()
        .sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
        .retryOnConnectionFailure(true)
        .hostnameVerifier { _, _ -> true }.build()

    } catch (e: Exception) {
        throw RuntimeException(e);
    }
}
p
I'd recommend checking where the request hangs when you are connected through mobile. May be useful to record network activity on your phone, if you call from Android