title: Setting A Cookie To A WebView using HttpClient and CookieManager
body: <p>I'm trying to pass a cookie along with a webview in Android. I tried using CookieManager and CookieSyncManager but to no avail. It keeps redirecting me to the login page of the website.</p>
  
  <p>Here is my code.</p>
  
  <pre><code>public class RequestSessionState extends AsyncTask&lt;String, Integer, String&gt; {
  
      String cookieString;
  
      @Override
      protected String doInBackground(String... params) {
          // TODO Auto-generated method stub
  
          DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
  
          HttpPost postCredentials = new HttpPost("https://www.mywebsite.com/users/login?url=users%2Flogin");
  
          List&lt;NameValuePair&gt; credentialsList = new ArrayList&lt;NameValuePair&gt;  ();
          credentialsList.add(new BasicNameValuePair("data[User][email]",
                  params[0]));
          credentialsList.add(new BasicNameValuePair("data[User][password]",
                  params[1]));
          credentialsList.add(new BasicNameValuePair("_method", "POST"));
  
          try {
              postCredentials.setEntity(new UrlEncodedFormEntity(credentialsList));
              defaultHttpClient.execute(postCredentials);
  
              CookieSyncManager.createInstance(LoginActivity.this);
              CookieManager cookieManager = CookieManager.getInstance();
              cookieManager.setAcceptCookie(true);
              cookieManager.removeSessionCookie();
  
              SystemClock.sleep(2000);
  
              List&lt;Cookie&gt; cookies = defaultHttpClient.getCookieStore().getCookies();
              for (Cookie cookie : cookies) {
                  cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();
                  cookieManager.setCookie("https://www.mywebsite.com/path", cookieString);
                  System.out.println("COOKIE ----------------- " + cookieString);
              }
              CookieSyncManager.getInstance().sync();
  
          } catch (UnsupportedEncodingException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (ClientProtocolException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          return cookieString;
      }
  
      @Override
      protected void onPostExecute(String result) {
          // TODO Auto-generated method stub
          super.onPostExecute(result);
          Editor addToSessionState = sessionState.edit();
          addToSessionState.putString("cookie", result);
          addToSessionState.commit();
  
          Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
          startActivity(intent);
      }
  }
  </code></pre>
  
  <p>and here is the onCreate in my next Activity:</p>
  
  <pre><code>setContentView(R.layout.activity_home);
      consolePageWebView = new WebView(HomeActivity.this);
      wvLayout = (RelativeLayout) findViewById(R.id.webViewLayout);
  
      LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
      consolePageWebView.setLayoutParams(params);
      wvLayout.addView(consolePageWebView);
  
  
      sessionState = getSharedPreferences("Session State", Context.MODE_PRIVATE);
      String cookieString = sessionState.getString("cookie", null);
  
      WebSettings webSettings = consolePageWebView.getSettings();
      webSettings.setJavaScriptEnabled(true);
      WebViewClient wvClient = new WebViewClient();
      consolePageWebView.setWebViewClient(wvClient);
  
      HashMap&lt;String, String&gt; headerMap = new HashMap&lt;String, String&gt;();
      headerMap.put("Cookie", cookieString);
      consolePageWebView.loadUrl("https://www.mywebsite.com/path", headerMap);        
  </code></pre>