优化自定义cookie设置

e2ba018061
pull/440/head
AndyBernie 4 years ago
parent deeeaa3b53
commit 0a4b2018c7
  1. 2
      app/src/main/java/io/legado/app/help/http/CookieStore.kt
  2. 30
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@ -63,7 +63,7 @@ object CookieStore : CookiePersistor {
return cookieMap return cookieMap
} }
private fun mapToCookie(cookieMap: Map<String, String>?): String? { fun mapToCookie(cookieMap: Map<String, String>?): String? {
if (cookieMap == null || cookieMap.isEmpty()) { if (cookieMap == null || cookieMap.isEmpty()) {
return null return null
} }

@ -281,7 +281,13 @@ class AnalyzeUrl(
fun getResponse(tag: String): Call<String> { fun getResponse(tag: String): Call<String> {
val cookie = CookieStore.getCookie(tag) val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) { if (cookie.isNotEmpty()) {
headerMap["Cookie"] += ";${cookie}" val cookieMap = CookieStore.cookieToMap(cookie)
val customCookieMap = CookieStore.cookieToMap(headerMap.get("Cookie") ?: "")
cookieMap.putAll(customCookieMap)
val newCookie = CookieStore.mapToCookie(cookieMap)
newCookie?.let {
headerMap.put("Cookie", it)
}
} }
return when { return when {
method == RequestMethod.POST -> { method == RequestMethod.POST -> {
@ -312,6 +318,16 @@ class AnalyzeUrl(
if (type != null) { if (type != null) {
return Res(url, StringUtils.byteToHexString(getResponseBytes(tag))) return Res(url, StringUtils.byteToHexString(getResponseBytes(tag)))
} }
val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) {
val cookieMap = CookieStore.cookieToMap(cookie)
val customCookieMap = CookieStore.cookieToMap(headerMap.get("Cookie") ?: "")
cookieMap.putAll(customCookieMap)
val newCookie = CookieStore.mapToCookie(cookieMap)
newCookie?.let {
headerMap.put("Cookie", it)
}
}
if (useWebView) { if (useWebView) {
val params = AjaxWebView.AjaxParams(url) val params = AjaxWebView.AjaxParams(url)
params.headerMap = headerMap params.headerMap = headerMap
@ -322,10 +338,6 @@ class AnalyzeUrl(
params.tag = tag params.tag = tag
return HttpHelper.ajax(params) return HttpHelper.ajax(params)
} }
val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) {
headerMap["Cookie"] += ";${cookie}"
}
val res = when { val res = when {
method == RequestMethod.POST -> { method == RequestMethod.POST -> {
if (fieldMap.isNotEmpty()) { if (fieldMap.isNotEmpty()) {
@ -356,7 +368,13 @@ class AnalyzeUrl(
if (tag != null) { if (tag != null) {
val cookie = CookieStore.getCookie(tag) val cookie = CookieStore.getCookie(tag)
if (cookie.isNotEmpty()) { if (cookie.isNotEmpty()) {
headerMap["Cookie"] += ";${cookie}" val cookieMap = CookieStore.cookieToMap(cookie)
val customCookieMap = CookieStore.cookieToMap(headerMap.get("Cookie") ?: "")
cookieMap.putAll(customCookieMap)
val newCookie = CookieStore.mapToCookie(cookieMap)
newCookie?.let {
headerMap.put("Cookie", it)
}
} }
} }
val response = when { val response = when {

Loading…
Cancel
Save