Network inspector not able to catch Network request - Android

199 Views Asked by At

I have used ktor CIO engine to make API calls. When I am trying to check network calls but URLs are not showing inside Android studio Network inspection. Performance testing team tried to intercept API calls using Fiddler tool but they are also unable to capture API calls.

Do I have to change any settings inside App.

I have added cleartextTrafficPermitted as true for testing for both System and Users.

 <base-config cleartextTrafficPermitted="true">
    <trust-anchors>
        <certificates src="system" />
        <certificates src="user" />
    </trust-anchors>
</base-config>

I tried adding cleartextTrafficPermitted inside AndroidManifest file also for testing. But nothing seems to work. Am I missing anything here?

Ktor implementation as below:

@Provides
@Singleton
fun provideHttpClient(): HttpClient = HttpClient(CIO) {

    expectSuccess = false
    install(ContentNegotiation) {
        gson()
    }

    install(Logging) {
        logger = Logger.DEFAULT
        level = LogLevel.ALL
    }
    
    install(HttpTimeout) {
        requestTimeoutMillis = 120000
        connectTimeoutMillis = 120000
        socketTimeoutMillis = 120000
    }

    engine {
        https {
            trustManager = @SuppressLint("CustomX509TrustManager")
            object : X509TrustManager {
                @SuppressLint("TrustAllX509TrustManager")
                override fun checkClientTrusted(p0: Array<out X509Certificate>?, p1: String?) {
                }

                @SuppressLint("TrustAllX509TrustManager")
                override fun checkServerTrusted(p0: Array<out X509Certificate>?, p1: String?) {
                }

                override fun getAcceptedIssuers(): Array<X509Certificate>? = null
            }
        }
    }

    HttpResponseValidator {
        validateResponse { response: HttpResponse ->
            val statusCode = response.status.value

            println("HTTP status: $statusCode")

            when (statusCode) {
                in 300..399 -> throw RedirectResponseException(response, statusCode.toString())
                in 400..499 -> throw ClientRequestException(response, statusCode.toString())
                in 500..599 -> throw ServerResponseException(response, statusCode.toString())
            }

            if (statusCode >= 600) {
                throw ResponseException(response, statusCode.toString())
            }
        }

        handleResponseExceptionWithRequest { cause, request ->
            println("HTTP status: $cause")
            throw cause
        }
    }

}
1

There are 1 best solutions below

1
amid aliaghazadeh On

I used this code and saw network requests in my network inspector

NetworkClient().httpClient {
        defaultRequest {
            url {
                protocol = URLProtocol.HTTPS
                host = provideServerUrl(get())
            }
        }
        install(ContentNegotiation) {
            json(
                Json {
                    ignoreUnknownKeys = true
                    prettyPrint = true
                    classDiscriminator = "class"
                }
            )
        }
        install(Logging) {
            logger = Logger.DEFAULT
            level = LogLevel.ALL
        }
    }
}

That network client is

expect class NetworkClient() {
fun httpClient(config: HttpClientConfig<*>.() -> Unit): HttpClient
}

and android-side implementation in KMM is:

    actual class NetworkClient actual constructor() {
    actual fun httpClient(config: HttpClientConfig<*>.() -> Unit): HttpClient {
        return HttpClient(OkHttp) {
            config(this)
            install(Logging){
                logger = object: Logger {
                    override fun log(message: String) {
                        Log.v("HTTP Client", message)
                    }
                }
                level = LogLevel.ALL
            }
            engine {
                config {
                    retryOnConnectionFailure(false)
                }
            }
        }
    }
  }