Cookies de Android sin CookieManager

I know that a similar question is posted here: Cookies de sesión de Android sin usar 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.

preguntado el 28 de agosto de 12 a las 09:08

1 Respuestas

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++;

          }

Respondido 28 ago 12, 09:08

What would I expect in "ssss"? - el bombardeo

its to store the cookie string - Athul Harikumar

Great. So, the result is: Set-Cookie: JSESSIONID=3B299C570A21D2A5E84673AC7718BA1C; Path=/cell What part of it do I need to "send back" when doing setRequestProperty? - el bombardeo

here 3B299C570A21D2A5E84673AC7718BA1C this is your session id or cookie - Athul Harikumar

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.