Square's meticulous HTTP client for Java and Kotlin
—
HTTP cookie storage and handling for session management and user preferences.
interface CookieJar {
fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>)
fun loadForRequest(url: HttpUrl): List<Cookie>
companion object {
val NO_COOKIES: CookieJar
}
}data class Cookie(
val name: String,
val value: String,
val expiresAt: Long,
val domain: String,
val path: String,
val secure: Boolean,
val httpOnly: Boolean,
val persistent: Boolean,
val hostOnly: Boolean
) {
class Builder {
fun name(name: String): Builder
fun value(value: String): Builder
fun expiresAt(expiresAt: Long): Builder
fun domain(domain: String): Builder
fun hostOnlyDomain(domain: String): Builder
fun path(path: String): Builder
fun secure(): Builder
fun httpOnly(): Builder
fun build(): Cookie
}
companion object {
fun parse(url: HttpUrl, setCookie: String): Cookie?
fun parseAll(url: HttpUrl, headers: Headers): List<Cookie>
}
}class MemoryCookieJar : CookieJar {
private val cookieStore = mutableMapOf<String, MutableList<Cookie>>()
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
val key = "${url.host}:${url.port}"
cookieStore[key] = cookies.toMutableList()
}
override fun loadForRequest(url: HttpUrl): List<Cookie> {
val key = "${url.host}:${url.port}"
return cookieStore[key] ?: emptyList()
}
}Install with Tessl CLI
npx tessl i tessl/maven-com-squareup-okhttp3--okhttp