清理App 全局的Cookie 這里就不記錄了膘流,主要記錄一下清理單個(gè)網(wǎng)頁的Cookie
private fun updateCookie(url: String) {
val cookieManager = CookieManager.getInstance()
val cookieGlob = cookieManager.getCookie(url);
if (cookieGlob != null) {
val cookies = cookieGlob.split(";")
cookies.map { cookieTuple ->
val cookieParts = cookieTuple.split("=")
val domainSet = getDomainSet(url)
domainSet.map { dm ->
cookieManager.setCookie(
dm,
cookieParts[0] + "=; Expires=Wed, 31 Dec 2015 23:59:59 GMT" // 這里是把 Cookie 的過期時(shí)間更新為過去的時(shí)間,如果有需求,這里還需要進(jìn)行適配一下格式。
)
}
}
cookieManager.flush()
}
}
private fun getDomainSet(domain: String): HashSet<String> {
val domainSet = HashSet<String>()
val host = Uri.parse(domain).host ?: ""
if (host.isNotEmpty()) {
domainSet.add(host)
domainSet.add(".$host")
if (host.indexOf(".") != host.lastIndexOf(".")) {
domainSet.add(host.substring(host.indexOf(".")))
}
}
return domainSet
}