Exchange cookies between requests in OkHttp

Tags: , ,



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?

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



Source: stackoverflow