title: Android sync cookies webview and httpclient
body: <p>I have a login webview and httpclient that need to confirm if the user is logged in.
  The problem is that the webview and the httpclient are using other cookies so the httpclient can't get the webview cookies.</p>
  
  <p>I read a lot of people questions and tutorials, but nothing worked. some of the things I read:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/11224454/android-share-session-between-webview-and-httpclient">ANDROID : Share session between Webview and httpclient</a></li>
  <li><a href="https://stackoverflow.com/questions/9795934/http-session-synchronization-between-webview-and-java-http-client-in-android">Http session synchronization between webview and java http client in Android</a></li>
  <li><a href="https://stackoverflow.com/questions/8733758/how-can-i-get-the-cookies-from-httpclient">How can I get the cookies from HttpClient?</a></li>
  </ul>
  
  <p>I read few other tutorials on Android Development and other websites but nothing worked.</p>
  
  <p>another post: <a href="https://stackoverflow.com/questions/28052461/syncing-webview-with-httpclient">https://stackoverflow.com/questions/28052461/syncing-webview-with-httpclient</a></p>
  
  <p><strong>The problem is that the cookies won't sync.</strong></p>
  
  <p>something I tried:</p>
  
  <pre><code>        WebView webview;
          webview = (WebView) rootView.findViewById(R.id.webview);
  
          webview.setWebViewClient(new WebViewClient() {
              public void onPageFinished(WebView view, String url) {
                  CookieSyncManager.getInstance().sync();
  
              }
          });
          webview.getSettings().setDomStorageEnabled(true);
          webview.getSettings().setJavaScriptEnabled(true);
          webview.getSettings().setUseWideViewPort(true);
          webview.setWebChromeClient(new WebChromeClient());
          webview.loadUrl("http://www.klh-dev.com/lehava/lehava/system/mlogin.php");
  </code></pre>
  
  <p>and some more:</p>
  
  <pre><code>   public String IsLoggedIn() {
              new Thread(new Runnable() {
                  @Override
                  public void run() {
                      HttpClient client=new DefaultHttpClient();
  
  
  
                      HttpGet get=new HttpGet(url);
                       ResponseHandler&lt;String&gt; responseHandler = new BasicResponseHandler();
                      try {
                          response_str=client.execute(get,responseHandler);
                          System.out.println(response_str);
                      } catch (ClientProtocolException e) {
                          e.printStackTrace();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
  
                      Cookie sessionInfo;
                      List&lt;Cookie&gt; cookies = client.getCookieStore().getCookies();
  
                      if (! cookies.isEmpty()){
                              CookieSyncManager.createInstance(getApplicationContext());
                              CookieManager cookieManager = CookieManager.getInstance();
  
                              for(Cookie cookie : cookies){
                                      sessionInfo = cookie;
                                      String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();
                                      cookieManager.setCookie(URLn, cookieString);
                                      CookieSyncManager.getInstance().sync();
                              }
                      }
                  }
              }).start();
              return response_str;
     }
  </code></pre>
  
  <p><strong>*The httpget return 1 or 0</strong></p>
  
  <p><strong>I want to take cookies from webview and use them in my httpclient request</strong></p>
  
  <p><strong>EDIT (added darpan's answer):</strong> </p>
  
  <pre><code>    public static BasicCookieStore getCookieStore(String cookies, String domain) {
          String[] cookieValues = cookies.split(";");
          BasicCookieStore cs = new BasicCookieStore();
  
          BasicClientCookie cookie;
          for (int i = 0; i &lt; cookieValues.length; i++) {
              String[] split = cookieValues[i].split("=");
              if (split.length == 2)
                  cookie = new BasicClientCookie(split[0], split[1]);
              else
                  cookie = new BasicClientCookie(split[0], null);
  
              cookie.setDomain(domain);
              cs.addCookie(cookie);
          }
          return cs;
  
          }
  
      public String IsLoggedIn() {
          new Thread(new Runnable() {
              @Override
              public void run() {
  
  
  
  
                       String cookies = CookieManager.getInstance().getCookie("http://klh-dev.com/lehava/lehava/system/isloggedin.php");
                       BasicCookieStore lCS = getCookieStore(cookies, "klh-dev.com");
  
                       HttpContext localContext = new BasicHttpContext();
                       DefaultHttpClient httpclient = new DefaultHttpClient();
                       httpclient.setCookieStore(lCS);
                       localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);
  
                  HttpGet get=new HttpGet("http://klh-dev.com/lehava/lehava/system/isloggedin.php");
                   ResponseHandler&lt;String&gt; responseHandler = new BasicResponseHandler();
  
  
  
                  try {
                      result=httpclient.execute(get,localContext);
                      response_str = EntityUtils.toString(result.getEntity());
                      System.out.println(response_str);
                  } catch (ClientProtocolException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
  
  
              }
          }).start();
          return response_str;
      }
  }
  </code></pre>
  
  <p><strong>EDIT2: Finally works!!</strong>
  This is my code:</p>
  
  <pre><code>public static String IsLoggedIn() {
      new Thread(new Runnable() {
          @Override
          public void run() {
                   String cookies = CookieManager.getInstance().getCookie(getUrl);
                   BasicCookieStore lCS = getCookieStore(cookies, "klh-dev.com");
  
                   HttpContext localContext = new BasicHttpContext();
                   DefaultHttpClient httpclient = new DefaultHttpClient();
                   httpclient.setCookieStore(lCS);
                   localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);
  
              HttpGet get = new HttpGet("http://klh-dev.com/lehava/lehava/system/isloggedin.php");
  
  
              try {
                  result=httpclient.execute(get,localContext);
                  response_str = EntityUtils.toString(result.getEntity());
                  System.out.println(response_str);
                  ((MainActivity) getContext).UpdateMenu();
              } catch (ClientProtocolException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
          }
      }).start();
      return response_str;
  }
  </code></pre>
  
  <p>and about the getUrl variable.
  I had to set a global variable like this:</p>
  
  <pre><code>private static String getUrl;
  
  public String getUrl() {
      return getUrl;
  }  
  </code></pre>
  
  <p>On every fragment I had to add onPageFinished:  <code>getUrl = view.getUrl();</code></p>
  
  <p>Thank you.</p>