title: Android Session cookies without using CookieManager
body: <p>My application makes multiple web calls in order to get authentication. I need to store this session in a cookie. I wanted to use Cookie Manager but after doing some research, I found out it is only available to API 9 and above and my application needs to be backward compatible. </p>
  
  <p>I make my web connections using HTTPURLConnection to a secure HTTPS. Quick example of my code</p>
  
  <pre><code>    public String iStream_to_String(InputStream is)
  {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is), 4096);
      String line;
      StringBuilder sb = new StringBuilder();
      try
      {
          while ((line = rd.readLine()) != null)
          {
              sb.append(line);
          }
          rd.close();
  
      } catch (IOException e)
      {
          e.printStackTrace();
      }
      String contentOfMyInputStream = sb.toString();
      return contentOfMyInputStream;
  }
  
  final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier()
  {
      public boolean verify(String hostname, SSLSession session)
      {
          return true;
      }
  };
  
  /**
   * Trust every server - dont check for any certificate
   */
  private static void trustAllHosts()
  {
      // Create a trust manager that does not validate certificate chains
      TrustManager[] trustAllCerts = new TrustManager[]
      { new X509TrustManager()
      {
          public java.security.cert.X509Certificate[] getAcceptedIssuers()
          {
              return new java.security.cert.X509Certificate[]
              {};
          }
  
          public void checkClientTrusted(X509Certificate[] chain,
                  String authType) throws CertificateException
          {
          }
  
          public void checkServerTrusted(X509Certificate[] chain,
                  String authType) throws CertificateException
          {
          }
      } };
  
      // Install the all-trusting trust manager
      try
      {
          SSLContext sc = SSLContext.getInstance("TLS");
          sc.init(null, trustAllCerts, new java.security.SecureRandom());
          HttpsURLConnection
                  .setDefaultSSLSocketFactory(sc.getSocketFactory());
      } catch (Exception e)
      {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Then I make a request like so</p>
  
  <pre><code>    try
      {
          url = new URL(url1);
  
  
  
          trustAllHosts();
             HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
         https.setHostnameVerifier(DO_NOT_VERIFY);
          http = https;
  
          InputStream in = new BufferedInputStream(http.getInputStream());
  
          sAuthenticateP1 = iStream_to_String(in);
          in.close();
  
  
      } catch (Exception e)
      {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>The full authentication is done in 4 steps. I need to have it so the session is remembered throughout the 4 steps. Seeing I can't use CookieManager, I have been looking around for other ways of doing this, but can't seem to find any. Would anyone be able to point me in the right direction.</p>
  
  <p>Thanks in advance!!</p>