0

I know that a similar question is posted here: Android Session cookies without using CookieManager However, I can not get it to work.

 URL urlObj = new URL(urlPath);
    conn = (HttpURLConnection) urlObj.openConnection();

    if (urlPath.toLowerCase().startsWith("https:")) {
        initializeHttpsConnection((HttpsURLConnection) conn);
    }
    conn.setRequestMethod("POST");


        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.connect();
        // Send body data
        os = conn.getOutputStream();
        os.write(bodyData);
        // Must flush and close to make sure all the data is sent.
        os.flush();
        os.close();
        // Get response
        in = conn.getInputStream();
        System.out.println("Initial set of cookies:");

        String cookie = conn.getRequestProperty("Cookie");

// Map> rp = conn.getRequestProperties(); if (cookie != null && cookie.length() > 0) { _cookie = cookie; Log.v("cookie2", _cookie); }

"cookie" is always null.

However, if I do include CookieManger (and run it with 2.3.3+) then cookie has the required value.

1 Answer 1

1

try this for http url con:

  while ((headerfields = connection.getHeaderField(i)) != null) {
          String key = connection.getHeaderFieldKey(i);
          if (key.equalsIgnoreCase("SET-COOKIE")) {
         ssss= (((key==null) ? "" : key + ": ") + headerfields);}
          i++;

          }
4
  • What would I expect in "ssss"?
    – theblitz
    Aug 28, 2012 at 9:12
  • its to store the cookie string Aug 28, 2012 at 9:14
  • Great. So, the result is: Set-Cookie: JSESSIONID=3B299C570A21D2A5E84673AC7718BA1C; Path=/cell What part of it do I need to "send back" when doing setRequestProperty?
    – theblitz
    Aug 28, 2012 at 11:37
  • here 3B299C570A21D2A5E84673AC7718BA1C this is your session id or cookie Aug 28, 2012 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.