pull/2046/head
kunfei 2 years ago
parent f721ff79af
commit 9098c4a27e
  1. 24
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  2. 2
      app/src/main/java/io/legado/app/help/http/CookieStore.kt
  3. 16
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt

@ -232,33 +232,45 @@ interface JsExtensions {
* js实现重定向拦截,网络访问get
*/
fun get(urlStr: String, headers: Map<String, String>): 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<String, String>): 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<String, String>): 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
}
/**

@ -82,7 +82,7 @@ object CookieStore : CookieManager {
}
override fun mapToCookie(cookieMap: Map<String, String>?): String? {
if (cookieMap == null || cookieMap.isEmpty()) {
if (cookieMap.isNullOrEmpty()) {
return null
}
val builder = StringBuilder()

@ -24,13 +24,21 @@ val cookieJar by lazy {
}
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
cookies.forEach {
//CookieStore.replaceCookie(url.toString(), "${it.name}=${it.value}")
if (cookies.isEmpty()) return
//临时保存 书源启用cookie选项再添加到数据库
val domain = NetworkUtils.getSubDomain(url.toString())
CacheManager.putMemory("${domain}_cookieJar", "${it.name}=${it.value}")
val cookieBuilder = StringBuilder()
cookies.forEach {
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())
}
}
}

Loading…
Cancel
Save