title: Sharing cookies from webview with BasicHttpRequest on Android
body: <p>I'm having trouble sending cookies as part of an http get.  First I go to a login page within a webview which gives me a cookie.  I have checked and the cookie is being stored in the CookieManager.  Then I use a BasicHttpRequest to get a particular URL from the same domain.  I would expect the cookie that I got from the login to be attached to my headers for the get, but looking at it in Wireshark it's not there.  I've googled and read a lot of similar questions and have made sure that:</p>
  
  <ul>
  <li>I am using the CookieSyncManager so I'd hope my cookies from the session would persist.  I don't think it's a problem with the CookieSyncManager being asynchronous because I keep hitting the URL every 5 seconds and the cookie is never added.</li>
  <li>I suspect I need to tell my http request about my cookie store, but the solutions I've googled don't compile for me.  It looks like I want to do something looking like context.setAttribute(ClientContext.COOKIE_STORE, this.cookieStore), but I can't figure out how to get the default CookieStore from the CookieManager.  Some code seems to call cookieManager.getCookieStore() but that doesn't compile for me on Android.  Looking at the docs I can't see a way to get the CookieStore which seems mad - am I missing something obvious?  </li>
  </ul>
  
  <p>My code to start up the login page in my webview looks like:</p>
  
  <pre><code> public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      // use cookies to remember a logged in status 
      CookieSyncManager.createInstance(this);
      CookieSyncManager.getInstance().startSync();
  
      //not sure if I need to do this
      CookieManager cookie_manager = CookieManager.getInstance();
      cookie_manager.setAcceptCookie(true);
  
      webview = new WebView(this);
      webview.getSettings().setJavaScriptEnabled(true);
      webview.setWebViewClient(new HelloWebViewClient()); // if user clicks on a url we need to steal that click, also steal the back button
      webview.loadUrl("http://"+my_server+"/api/v1/login");
      setContentView(webview);
  </code></pre>
  
  <p>Then my code to check the cookie is there looks like:</p>
  
  <pre><code>public static boolean CheckAuthorised() {
      CookieSyncManager.getInstance().sync();
      CookieManager cookie_manager = CookieManager.getInstance();
  
      String cookie_string = cookie_manager.getCookie("http://"+my_server+"/api/v1/login");
      System.out.println("lbp.me cookie_string: " + cookie_string);
  
      if(cookie_string != null)
      {
          String[] cookies = cookie_string.split(";");
          for (String cookie : cookies)
          {
              if(cookie.matches("API_AUTH=.*"))
              {
                  // maybe we need to store the cookie for the root of the domain?
                  cookie_manager.setCookie("http://"+my_server, cookie_string);
                  // maybe we need to store the cookie for the url we're actually going to access?
                  cookie_manager.setCookie("http://"+my_server+"/api/v1/activity", cookie_string);    
  
                  CookieSyncManager.getInstance().sync();
                  return true;
              }
          }
      }
  
      return false;
  }
  </code></pre>
  
  <p>And to actually make the http request I do</p>
  
  <pre><code>public static HttpResponse getMeAWebpage(String host_string, int port, String url)
          throws Exception {
      HttpParams params = new BasicHttpParams();
      HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
      HttpProtocolParams.setContentCharset(params, "UTF-8");
      HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
      HttpProtocolParams.setUseExpectContinue(params, true);
  
      BasicHttpProcessor httpproc = new BasicHttpProcessor();
      // Required protocol interceptors
      httpproc.addInterceptor(new RequestContent());
      httpproc.addInterceptor(new RequestTargetHost());
      // Recommended protocol interceptors
      httpproc.addInterceptor(new RequestConnControl());
      httpproc.addInterceptor(new RequestUserAgent());
      httpproc.addInterceptor(new RequestExpectContinue());
  
      HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
  
      HttpContext context = new BasicHttpContext(null);
      // HttpHost host = new HttpHost("www.svd.se", 80);
      HttpHost host = new HttpHost(host_string, port);
  
      DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
      ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
  
      context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
      context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
      //CookieManager cookie_manager = CookieManager.getInstance();
      //CookieStore cookie_store = cookie_manager.getCookieStore(); //The method getCookieStore() is undefined for the type CookieManager
      //context.setAttribute(ClientContext.COOKIE_STORE, cookie_store);
  
      HttpResponse response = null;
  
      try {
          if (!conn.isOpen()) {
              Socket socket = new Socket(host.getHostName(), host.getPort());
              conn.bind(socket, params);
          }
  
          BasicHttpRequest request = new BasicHttpRequest("GET", url);
          System.out.println("&gt;&gt; Request URI: "
                  + request.getRequestLine().getUri());
          System.out.println("&gt;&gt; Request: "
                  + request.getRequestLine());
  
          request.setParams(params);
          httpexecutor.preProcess(request, httpproc, context);
          response = httpexecutor.execute(request, conn, context);
          response.setParams(params);
          httpexecutor.postProcess(response, httpproc, context);
  
          String ret = EntityUtils.toString(response.getEntity());
          System.out.println("&lt;&lt; Response: " + response.getStatusLine());
          System.out.println(ret);
          System.out.println("==============");
          if (!connStrategy.keepAlive(response, context)) {
              conn.close();
          } else {
              System.out.println("Connection kept alive...");
          }
      } catch(UnknownHostException e) {
          System.out.println("UnknownHostException"); 
      } catch (HttpException e) {
          System.out.println("HttpException"); 
      } finally {
          conn.close();
      }
  
      return response;
  }
  </code></pre>
  
  <p>Thank you for reading this far!  Any suggestions gratefully received,</p>
  
  <p>Amy</p>