我知道这里发布了一个类似的问题: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 具有所需的值。