Skip to main content

How to handle cookies in OKHTTP in Kotlin Android

How to handle cookies in OKHTTP in Kotlin Android.

Here's a step-by-step tutorial on how to handle cookies in OKHTTP in Kotlin for Android:

Step 1: Add Dependencies

Add the following dependencies to your app-level build.gradle file:

dependencies {
// Other dependencies
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
}

Step 2: Create an OkHttpClient instance

In your code, create an instance of OkHttpClient to use for making HTTP requests:

val client = OkHttpClient()

Enable the cookie manager in the OkHttpClient instance by creating a CookieJar and setting it in the builder:

val cookieManager = CookieManager()
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL)

val builder = OkHttpClient.Builder()
.cookieJar(JavaNetCookieJar(cookieManager))
val client = builder.build()

Step 4: Make a Request

Now, you can use the client to make HTTP requests. For example, to make a GET request:

val request = Request.Builder()
.url("https://example.com")
.build()

val response = client.newCall(request).execute()

Step 5: Save Cookies from Response

To save the cookies received in the response, you can implement a custom CookieJar. Create a class that implements the CookieJar interface and override the saveFromResponse method:

class MyCookieJar : CookieJar {

private val cookieStore = HashMap<String, MutableList<Cookie>>()

override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
cookieStore[url.host] = cookies.toMutableList()
}

// Implement other methods if needed

}

Step 6: Set Custom CookieJar in OkHttpClient

Set the custom CookieJar in the OkHttpClient builder:

val cookieJar = MyCookieJar()

val builder = OkHttpClient.Builder()
.cookieJar(cookieJar)
val client = builder.build()

Step 7: Attach Cookies to Request

To attach the saved cookies to subsequent requests, you can implement a custom Interceptor. Create a class that implements the Interceptor interface and override the intercept method:

class AddCookiesInterceptor(private val cookieJar: MyCookieJar) : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
val requestBuilder = chain.request().newBuilder()

val cookies = cookieJar.loadForRequest(chain.request().url)
for (cookie in cookies) {
requestBuilder.addHeader("Cookie", cookie.toString())
}

return chain.proceed(requestBuilder.build())
}

// Implement other methods if needed

}

Step 8: Add Custom Interceptor to OkHttpClient

Add the custom Interceptor to the OkHttpClient builder:

val cookieJar = MyCookieJar()
val addCookiesInterceptor = AddCookiesInterceptor(cookieJar)

val builder = OkHttpClient.Builder()
.cookieJar(cookieJar)
.addInterceptor(addCookiesInterceptor)
val client = builder.build()

Step 9: Make Requests with Attached Cookies

Now, when you make subsequent requests using the client, the saved cookies will be attached automatically:

val request = Request.Builder()
.url("https://example.com")
.build()

val response = client.newCall(request).execute()

That's it! You've successfully learned how to handle cookies in OKHTTP in Kotlin for Android.