In my application I send a POST request to a server, and receive a response from the server. And from the response I gather different cookies, user information specifically. So,
This code worked fine for me prior to KitKat 4.4 update -
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//handle cookies
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
It broke after 4.4.2 (at least that's when I noticed it), and cookies were no longer received. Simply moving the CookieManager and CookieHandler before opening the urlConnection fixed it again .. bizarre that it worked before! eg.
//handle cookies
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
I fixed the problem. I switched from HttpUrlConnection and other java.net stuff to AndroidHttpClient and other Apache Http classes. Cookies are now retrieved from Android API 19.
[EDIT] I used AndroidHttpClient (http://developer.android.com/reference/android/net/http/AndroidHttpClient.html) and I followed a couple Apache Http tutorials. So after I changed the code around a little bit, it looks like this:
private AndroidHttpClient httpClient; //Android Client, Uses User-Agent, and executes request
private HttpContext httpContext; //Contains CookieStore that is sent along with request
private CookieStore cookieStore; //Holds cookies from server
private HttpPost httpPost; //Contains message to be sent to client
private final String USER_AGENT = System.getProperty("http.agent"); //Unique User-Agent of current device
//First call whenever connecting across the user's network
private void establishConnection() {
cookieStore = new BasicCookieStore();
httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); //Attach CookieStore to the HttpContext
getCookies(); //Retrieve currently stored cookies
httpClient = AndroidHttpClient.newInstance(USER_AGENT);
httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setHeader("Cookie", sessionSeedCookie + ";" + sessionUidCookie + ";" + userLoginCookie + ";" + sPrefCookie);
}
//Called after communication is complete
private void disconnectAll() {
httpClient.close();
}
//Sends out POST request and returns an InputStream
private InputStream processRequest(String query) throws IOException{
httpPost.setEntity(new StringEntity(query)); //wraps the query into a String entity
HttpResponse response = httpClient.execute(httpPost, httpContext); //Executes the request along with the cookie store
Log.i(TAG, "Server Response Code: " + response.getStatusLine().getStatusCode()); //Reads response code from server
updateCookies();
return response.getEntity().getContent();
}
//Logins User into Walkntrade
public String login(String email, String password) throws IOException {
establishConnection(); //Instantiate all streams and opens the connection
String query= "intent=login&password="+password+"&email="+email+"&rememberMe=true";
InputStream inputStream = processRequest(query);
String serverResponse = readInputAsString(inputStream); //Reads message response from server
disconnectAll();
return serverResponse;
}