0

我知道这里发布了一个类似的问题:Android Session cookies without using CookieManager 但是,我无法让它工作。

 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”始终为空。

但是,如果我确实包含 CookieManger(并使用 2.3.3+ 运行它),那么 cookie 具有所需的值。

4

1 回答 1

1

试试这个 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++;

          }
于 2012-08-28T08:59:30.443 回答