I'm developing a native Android manga reader in Kotlin, and I'm investigating a session-management problem involving Cloudflare-protected websites.
I examined my current implementation and found that it uses both Android WebView and OkHttp:
- WebView has JavaScript and DOM storage enabled, and accepts first-party and third-party cookies.
- Cookies are retrieved through Android's CookieManager and manually attached to outgoing OkHttp requests.
- CookieManager.flush() is called after certain WebView operations.
- For one source, a Turnstile token is also retrieved through WebView and sent separately in an HTTP request header.
- When a request reports that browser verification is required, the application waits for the user to complete verification and then retries the operation.
Here is a simplified excerpt illustrating the cookie-sharing approach used in my implementation:
val cookieManager = CookieManager.getInstance()
// Read cookies stored by Android WebView
val cookies = cookieManager.getCookie(url).orEmpty()
val request = Request.Builder()
.url(url)
.apply {
if (cookies.isNotBlank()) {
header("Cookie", cookies)
}
}
.build()
client.newCall(request).execute().use { response ->
// Handle response
}
WebView initialization also includes:
val cookieManager = CookieManager.getInstance()
cookieManager.setAcceptCookie(true)
cookieManager.setAcceptThirdPartyCookies(webView, true)
Despite these mechanisms, verification is not consistently maintained. A website may work in an external browser, while requests from the application still receive a Cloudflare challenge. In some cases, verification succeeds but the challenge appears again when opening a chapter or requesting its images.
I'd like advice from developers experienced with Android networking and WebView:
- Is manually copying cookies from CookieManager into OkHttp requests a reliable approach, or are there important session details it does not preserve?
- How should cookie updates and expiration be synchronized between WebView and an HTTP client?
- Can differences in User-Agent, TLS fingerprints, request headers, or browser context cause a verified session to be rejected?
- Would maintaining a persistent WebView session be preferable to creating separate WebView instances for different operations?
- How can developers distinguish a session synchronization problem from Cloudflare rejecting an otherwise valid request?
- Are there architectural limitations that cannot be solved reliably at the application level?
I'm particularly interested in practical Android/Kotlin examples, established architectural patterns, and open-source implementations.
I'm not trying to bypass CAPTCHA or defeat anti-bot protections. The goal is to handle legitimate user verification and browser session state correctly.
Any technical explanations or references would be greatly appreciated.