https://kotlinlang.org logo
#ktor
Title
# ktor
j

Jeff

09/09/2021, 9:04 AM
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

Piotr Krzemiński

09/09/2021, 9:15 AM
does your server receive this request?
j

Jeff

09/09/2021, 9:23 AM
Nope
p

Piotr Krzemiński

09/09/2021, 9:55 AM
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

Jeff

09/09/2021, 10:11 AM
When I switch from data to wifi it works
But my data is working fine in other apps
p

Piotr Krzemiński

09/09/2021, 10:12 AM
do you try to fetch/send lots of data? maybe it just hits the timeout as it says?
j

Jeff

09/09/2021, 10:12 AM
Nope. Its just a basic request
p

Piotr Krzemiński

09/09/2021, 10:15 AM
what URL are you calling?
j

Jeff

09/09/2021, 10:16 AM
Its a private url for my backend
p

Piotr Krzemiński

09/09/2021, 10:17 AM
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

Jeff

09/09/2021, 10:19 AM
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

Piotr Krzemiński

09/09/2021, 12:49 PM
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
4 Views