From 9098c4a27eff8de7c2b854a1da69c5b4c638fdef Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 9 Jul 2022 20:07:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/JsExtensions.kt | 24 ++++++++++++++++--- .../io/legado/app/help/http/CookieStore.kt | 2 +- .../io/legado/app/help/http/HttpHelper.kt | 16 +++++++++---- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index a7c4f3927..5b411bb6c 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -232,33 +232,45 @@ interface JsExtensions { * js实现重定向拦截,网络访问get */ fun get(urlStr: String, headers: Map): Connection.Response { - return Jsoup.connect(urlStr) + val response = Jsoup.connect(urlStr) .sslSocketFactory(SSLHelper.unsafeSSLSocketFactory) .ignoreContentType(true) .followRedirects(false) .headers(headers) .method(Connection.Method.GET) .execute() + val cookies = response.cookies() + CookieStore.mapToCookie(cookies)?.let { + val domain = NetworkUtils.getSubDomain(urlStr) + CacheManager.putMemory("${domain}_cookieJar", it) + } + return response } /** * js实现重定向拦截,网络访问head,不返回Response Body更省流量 */ fun head(urlStr: String, headers: Map): Connection.Response { - return Jsoup.connect(urlStr) + val response = Jsoup.connect(urlStr) .sslSocketFactory(SSLHelper.unsafeSSLSocketFactory) .ignoreContentType(true) .followRedirects(false) .headers(headers) .method(Connection.Method.HEAD) .execute() + val cookies = response.cookies() + CookieStore.mapToCookie(cookies)?.let { + val domain = NetworkUtils.getSubDomain(urlStr) + CacheManager.putMemory("${domain}_cookieJar", it) + } + return response } /** * 网络访问post */ fun post(urlStr: String, body: String, headers: Map): Connection.Response { - return Jsoup.connect(urlStr) + val response = Jsoup.connect(urlStr) .sslSocketFactory(SSLHelper.unsafeSSLSocketFactory) .ignoreContentType(true) .followRedirects(false) @@ -266,6 +278,12 @@ interface JsExtensions { .headers(headers) .method(Connection.Method.POST) .execute() + val cookies = response.cookies() + CookieStore.mapToCookie(cookies)?.let { + val domain = NetworkUtils.getSubDomain(urlStr) + CacheManager.putMemory("${domain}_cookieJar", it) + } + return response } /** diff --git a/app/src/main/java/io/legado/app/help/http/CookieStore.kt b/app/src/main/java/io/legado/app/help/http/CookieStore.kt index 625f4d1d3..ca0ab8109 100644 --- a/app/src/main/java/io/legado/app/help/http/CookieStore.kt +++ b/app/src/main/java/io/legado/app/help/http/CookieStore.kt @@ -82,7 +82,7 @@ object CookieStore : CookieManager { } override fun mapToCookie(cookieMap: Map?): String? { - if (cookieMap == null || cookieMap.isEmpty()) { + if (cookieMap.isNullOrEmpty()) { return null } val builder = StringBuilder() diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index e6e1a672f..f0490af17 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -24,12 +24,20 @@ val cookieJar by lazy { } override fun saveFromResponse(url: HttpUrl, cookies: List) { + if (cookies.isEmpty()) return + //临时保存 书源启用cookie选项再添加到数据库 + val cookieBuilder = StringBuilder() cookies.forEach { - //CookieStore.replaceCookie(url.toString(), "${it.name}=${it.value}") - //临时保存 书源启用cookie选项再添加到数据库 - val domain = NetworkUtils.getSubDomain(url.toString()) - CacheManager.putMemory("${domain}_cookieJar", "${it.name}=${it.value}") + if (it.value != "") { + cookieBuilder.append(it.name) + .append("=") + .append(it.value) + .append(";") + } } + cookieBuilder.deleteCharAt(cookieBuilder.lastIndexOf(";")) + val domain = NetworkUtils.getSubDomain(url.toString()) + CacheManager.putMemory("${domain}_cookieJar", cookieBuilder.toString()) } }