title: Use stored Cookie in persistentcookiejar or some other container for okhttp requests in android
body: <p><strong>This question basically wants to know how to use stored cookies for subsequent request.</strong> The long text below is just for example. <em>Basic question is how to use the same cookie for all requests on a particular website.</em></p>
  
  <p>Basically I am trying to reach the login page of a website within the android app. The website works the following way.</p>
  
  <p>There are 3 urls to consider.</p>
  
  <p>1 -> "<a href="http://www.example.com/timeoutPage" rel="nofollow noreferrer">http://www.example.com/timeoutPage</a>"</p>
  
  <p>2 -> "<a href="http://www.example.com/mainPage" rel="nofollow noreferrer">http://www.example.com/mainPage</a>"</p>
  
  <p>3 -> "<a href="http://www.example.com/loginPage" rel="nofollow noreferrer">http://www.example.com/loginPage</a>"</p>
  
  <p><strong>The two main points to consider are</strong></p>
  
  <p>(1) If we directly go to the 3rd url (loginPage), it redirects to the 1st url(timeoutPage). The timeoutPage has a button to go to the mainPage.</p>
  
  <p>(2) If we go to the 2nd url (mainPage), it gives us a cookie. Now, after getting the cookie, when we visit the 3rd url (loginPage),  we are able to access it. The loginPage has a captcha so it's essential to visit it(loginPage) in order to login.</p>
  
  <p><strong>Without the cookie, which is given at visiting 2nd url(mainPage), we cannot directly access 3rd url(loginPage).</strong></p>
  
  <p>so what i am doing is to make a ClearableCookieJar and <em>attach</em> it to OkHttpClient.</p>
  
  <pre><code>OkHttpClient client;
  ClearableCookieJar cookieJar;
  </code></pre>
  
  <pre><code>CookieManager cookieManager = new CookieManager();
  cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
  cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(this));
  </code></pre>
  
  <pre><code>client = new OkHttpClient.Builder()
                  .cookieJar(cookieJar)
                  .build();
  </code></pre>
  
  <pre><code>Request request = new Request.Builder()
                  .url("http://www.example.com/mainPage")
                  .build();
  
          client.newCall(request).enqueue(new Callback() {
              @Override
              public void onFailure(Call call, IOException e) {
                  e.printStackTrace();
                  textView.setText("Failed to get response");
              }
  
              @Override
              public void onResponse(Call call, Response response) throws IOException {
                  if(response.isSuccessful()){
                      final String myResponse = response.body().string();
                      MainActivity.this.runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
                              textView.setText(myResponse);
                          }
                      });
                  }
              }
          });
  </code></pre>
  
  <p>Everthing is fine till here as i am able to print the html of mainPage in my textview.
  Problem starts here when i make another request for the loginPage.</p>
  
  <pre><code>request = new Request.Builder()
                  .url("http://www.example.com/loginPage")
                  .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0")
                  .build();
  
          client.newCall(request).enqueue(new Callback() {
              @Override
              public void onFailure(Call call, IOException e) {
                  e.printStackTrace();
                  textView.setText("Failed to get response");
              }
  
              @Override
              public void onResponse(Call call, Response response) throws IOException {
                  if(response.isSuccessful()){
                      final String myResponse2 = response.body().string();
                      MainActivity.this.runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
                              textView.setText(myResponse2);
                          }
                      });
                  }
              }
          });
  </code></pre>
  
  <p>Here i again make  a request to the loginPage but i reach the timeoutPage. How should i make the request for the loginPage url so that my request sends the cookie which i stored in the cookieJar while making a request to mainPage. One way i thought of was </p>
  
  <pre><code>request = new Request.Builder()
                  .url("http://www.example.com/loginPage")
                  .addHeader("Cookie", cookieStringGivenAtMainPage)
                  .build();
  </code></pre>
  
  <p>But i don't know how to access that cookieStringGivenAtMainPage. How should i reach that loginPage? </p>
  
  <p><em>I print the html of the response to see if i reached the correct page.</em></p>