0
# Cookie Management
1
2
HTTP cookie storage and handling for session management and user preferences.
3
4
## CookieJar Interface
5
6
```kotlin { .api }
7
interface CookieJar {
8
fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>)
9
fun loadForRequest(url: HttpUrl): List<Cookie>
10
11
companion object {
12
val NO_COOKIES: CookieJar
13
}
14
}
15
```
16
17
## Cookie Data Class
18
19
```kotlin { .api }
20
data class Cookie(
21
val name: String,
22
val value: String,
23
val expiresAt: Long,
24
val domain: String,
25
val path: String,
26
val secure: Boolean,
27
val httpOnly: Boolean,
28
val persistent: Boolean,
29
val hostOnly: Boolean
30
) {
31
class Builder {
32
fun name(name: String): Builder
33
fun value(value: String): Builder
34
fun expiresAt(expiresAt: Long): Builder
35
fun domain(domain: String): Builder
36
fun hostOnlyDomain(domain: String): Builder
37
fun path(path: String): Builder
38
fun secure(): Builder
39
fun httpOnly(): Builder
40
fun build(): Cookie
41
}
42
43
companion object {
44
fun parse(url: HttpUrl, setCookie: String): Cookie?
45
fun parseAll(url: HttpUrl, headers: Headers): List<Cookie>
46
}
47
}
48
```
49
50
### Cookie Jar Implementation Example
51
52
```kotlin
53
class MemoryCookieJar : CookieJar {
54
private val cookieStore = mutableMapOf<String, MutableList<Cookie>>()
55
56
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
57
val key = "${url.host}:${url.port}"
58
cookieStore[key] = cookies.toMutableList()
59
}
60
61
override fun loadForRequest(url: HttpUrl): List<Cookie> {
62
val key = "${url.host}:${url.port}"
63
return cookieStore[key] ?: emptyList()
64
}
65
}
66
```