Skip to content
Advertisement

Exchange cookies between requests in OkHttp

I’m trying to scrape one website, and for that, I need to exchange the cookies and headers between all the requests.

The question is the following: how can I achieve such behaviour in a smart way, not by resetting the cookies and headers manually between the Request and Response objects each time?

Advertisement

Answer

You’ll need a CookieJar. There’s an in-memory implementation in the okhttp-urlconnection artifact.

See Automatic cookie handling with OkHttp 3 for examples

3 People found this is helpful
Advertisement