访问完成就保存cookie
This commit is contained in:
@@ -41,7 +41,10 @@ object CookieStore : CookieManager {
|
|||||||
*/
|
*/
|
||||||
override fun getCookie(url: String): String {
|
override fun getCookie(url: String): String {
|
||||||
val domain = NetworkUtils.getSubDomain(url)
|
val domain = NetworkUtils.getSubDomain(url)
|
||||||
CacheManager.getFromMemory("${domain}_cookie")?.let { return it }
|
CacheManager.getFromMemory("${domain}_cookie")?.let {
|
||||||
|
return it
|
||||||
|
}
|
||||||
|
|
||||||
val cookieBean = appDb.cookieDao.get(domain)
|
val cookieBean = appDb.cookieDao.get(domain)
|
||||||
val cookie = cookieBean?.cookie ?: ""
|
val cookie = cookieBean?.cookie ?: ""
|
||||||
CacheManager.putMemory(url, cookie)
|
CacheManager.putMemory(url, cookie)
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ class AnalyzeUrl(
|
|||||||
private var useWebView: Boolean = false
|
private var useWebView: Boolean = false
|
||||||
private var webJs: String? = null
|
private var webJs: String? = null
|
||||||
private val enabledCookieJar = source?.enabledCookieJar ?: false
|
private val enabledCookieJar = source?.enabledCookieJar ?: false
|
||||||
|
private val domain: String
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val urlMatcher = paramPattern.matcher(baseUrl)
|
val urlMatcher = paramPattern.matcher(baseUrl)
|
||||||
@@ -86,6 +87,7 @@ class AnalyzeUrl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
initUrl()
|
initUrl()
|
||||||
|
domain = NetworkUtils.getSubDomain(source?.getKey() ?: url)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -368,7 +370,7 @@ class AnalyzeUrl(
|
|||||||
}
|
}
|
||||||
val concurrentRecord = fetchStart()
|
val concurrentRecord = fetchStart()
|
||||||
try {
|
try {
|
||||||
setCookie(source?.getKey())
|
setCookie()
|
||||||
val strResponse: StrResponse
|
val strResponse: StrResponse
|
||||||
if (this.useWebView && useWebView) {
|
if (this.useWebView && useWebView) {
|
||||||
strResponse = when (method) {
|
strResponse = when (method) {
|
||||||
@@ -422,6 +424,7 @@ class AnalyzeUrl(
|
|||||||
}
|
}
|
||||||
return strResponse
|
return strResponse
|
||||||
} finally {
|
} finally {
|
||||||
|
saveCookie()
|
||||||
fetchEnd(concurrentRecord)
|
fetchEnd(concurrentRecord)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -463,7 +466,7 @@ class AnalyzeUrl(
|
|||||||
suspend fun getResponseAwait(): Response {
|
suspend fun getResponseAwait(): Response {
|
||||||
val concurrentRecord = fetchStart()
|
val concurrentRecord = fetchStart()
|
||||||
try {
|
try {
|
||||||
setCookie(source?.getKey())
|
setCookie()
|
||||||
@Suppress("BlockingMethodInNonBlockingContext")
|
@Suppress("BlockingMethodInNonBlockingContext")
|
||||||
val response = getProxyClient(proxy).newCallResponse(retry) {
|
val response = getProxyClient(proxy).newCallResponse(retry) {
|
||||||
addHeaders(headerMap)
|
addHeaders(headerMap)
|
||||||
@@ -486,6 +489,7 @@ class AnalyzeUrl(
|
|||||||
}
|
}
|
||||||
return response
|
return response
|
||||||
} finally {
|
} finally {
|
||||||
|
saveCookie()
|
||||||
fetchEnd(concurrentRecord)
|
fetchEnd(concurrentRecord)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -571,19 +575,17 @@ class AnalyzeUrl(
|
|||||||
/**
|
/**
|
||||||
* 设置cookie 优先级
|
* 设置cookie 优先级
|
||||||
* urlOption临时cookie > 数据库cookie = okhttp CookieJar保存在内存中的cookie
|
* urlOption临时cookie > 数据库cookie = okhttp CookieJar保存在内存中的cookie
|
||||||
*@param tag 书源url 缺省为传入的url
|
|
||||||
*/
|
*/
|
||||||
private fun setCookie(tag: String?) {
|
private fun setCookie() {
|
||||||
val domain = NetworkUtils.getSubDomain(tag ?: url)
|
val cookie = kotlin.run {
|
||||||
//书源启用保存cookie时 添加内存中的cookie到数据库
|
|
||||||
if (enabledCookieJar) {
|
if (enabledCookieJar) {
|
||||||
val key = "${domain}_cookieJar"
|
val key = "${domain}_cookieJar"
|
||||||
CacheManager.getFromMemory(key)?.let {
|
CacheManager.getFromMemory(key)?.let {
|
||||||
CookieStore.replaceCookie(domain, it)
|
return@run it
|
||||||
CacheManager.deleteMemory(key)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val cookie = CookieStore.getCookie(domain)
|
CookieStore.getCookie(domain)
|
||||||
|
}
|
||||||
if (cookie.isNotEmpty()) {
|
if (cookie.isNotEmpty()) {
|
||||||
val cookieMap = CookieStore.cookieToMap(cookie)
|
val cookieMap = CookieStore.cookieToMap(cookie)
|
||||||
val customCookieMap = CookieStore.cookieToMap(headerMap["Cookie"] ?: "")
|
val customCookieMap = CookieStore.cookieToMap(headerMap["Cookie"] ?: "")
|
||||||
@@ -594,11 +596,26 @@ class AnalyzeUrl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存cookie
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private fun saveCookie() {
|
||||||
|
//书源启用保存cookie时 添加内存中的cookie到数据库
|
||||||
|
if (enabledCookieJar) {
|
||||||
|
val key = "${domain}_cookieJar"
|
||||||
|
CacheManager.getFromMemory(key)?.let {
|
||||||
|
CookieStore.replaceCookie(domain, it)
|
||||||
|
CacheManager.deleteMemory(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*获取处理过阅读定义的urlOption和cookie的GlideUrl
|
*获取处理过阅读定义的urlOption和cookie的GlideUrl
|
||||||
*/
|
*/
|
||||||
fun getGlideUrl(): GlideUrl {
|
fun getGlideUrl(): GlideUrl {
|
||||||
setCookie(source?.getKey())
|
setCookie()
|
||||||
return GlideUrl(url, GlideHeaders(headerMap))
|
return GlideUrl(url, GlideHeaders(headerMap))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user