Fix Cronet not loading cookieJar on first request
This commit is contained in:
@@ -59,19 +59,18 @@ object CookieManager {
|
||||
val cookie = CookieStore.getCookie(domain)
|
||||
val requestCookie = request.header("Cookie")
|
||||
|
||||
mergeCookies(requestCookie, cookie)?.let { newCookie ->
|
||||
kotlin.runCatching {
|
||||
return request.newBuilder()
|
||||
.header("Cookie", newCookie)
|
||||
.build()
|
||||
}.onFailure {
|
||||
CookieStore.removeCookie(url)
|
||||
AppLog.put(
|
||||
"设置cookie出错,已清除cookie $domain cookie:$newCookie\n${it.localizedMessage}",
|
||||
it
|
||||
)
|
||||
}
|
||||
val newCookie = mergeCookies(requestCookie, cookie) ?: return request
|
||||
|
||||
kotlin.runCatching {
|
||||
return request.newBuilder()
|
||||
.header("Cookie", newCookie)
|
||||
.build()
|
||||
}.onFailure {
|
||||
CookieStore.removeCookie(url)
|
||||
val msg = "设置cookie出错,已清除cookie $domain cookie:$newCookie\n$it"
|
||||
AppLog.put(msg, it)
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,15 @@ package io.legado.app.lib.cronet
|
||||
import android.annotation.SuppressLint
|
||||
import android.os.Build
|
||||
import androidx.annotation.Keep
|
||||
import io.legado.app.help.http.CookieManager
|
||||
import io.legado.app.help.http.CookieManager.cookieJarHeader
|
||||
import io.legado.app.utils.printOnDebug
|
||||
import okhttp3.*
|
||||
import okhttp3.Call
|
||||
import okhttp3.CookieJar
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
import java.io.IOException
|
||||
|
||||
@Keep
|
||||
@@ -28,7 +35,12 @@ class CronetInterceptor(private val cookieJar: CookieJar) : Interceptor {
|
||||
builder.removeHeader("Keep-Alive")
|
||||
builder.removeHeader("Accept-Encoding")
|
||||
|
||||
val newReq = builder.build()
|
||||
var newReq = builder.build()
|
||||
|
||||
if (newReq.header(cookieJarHeader) != null) {
|
||||
newReq = CookieManager.loadRequest(newReq)
|
||||
}
|
||||
|
||||
return proceedWithCronet(newReq, chain.call(), chain.readTimeoutMillis())!!
|
||||
} catch (e: Exception) {
|
||||
cronetException = e
|
||||
|
||||
@@ -105,7 +105,7 @@ class AnalyzeUrl(
|
||||
private var retry: Int = 0
|
||||
private var useWebView: Boolean = false
|
||||
private var webJs: String? = null
|
||||
private val enabledCookieJar = source?.enabledCookieJar ?: false
|
||||
private val enabledCookieJar = source?.enabledCookieJar == true
|
||||
private val domain: String
|
||||
private var webViewDelayTime: Long = 0
|
||||
private val concurrentRateLimiter = ConcurrentRateLimiter(source)
|
||||
@@ -551,6 +551,8 @@ class AnalyzeUrl(
|
||||
}
|
||||
if (enabledCookieJar) {
|
||||
headerMap[CookieManager.cookieJarHeader] = "1"
|
||||
} else {
|
||||
headerMap.remove(CookieManager.cookieJarHeader)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user