I'm trying to fetch data from a server with this code
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT);
HttpRequestRetryHandler retryhandler = new DefaultHttpRequestRetryHandler(6, true);
httpClient.setHttpRequestRetryHandler(retryhandler);
BasicCookieStore cookieStore = new BasicCookieStore();
httpClient.setCookieStore(cookieStore);
if(Build.VERSION.SDK_INT>8){
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
}
HttpGet httpget = new HttpGet(URL);
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
...
It's working fine with all the devices I tried so far except the Samsung Galaxy SII (android 4.0.3) of my client. From the server response, it seems like the session cookies are not stored. I failed to reproduce this bug and everything I tried so far didn't solve it. Do you have any clue ?
EDIT: removed a bloc of code who was disabling the StrictMode policy (still having the issue tho)
I had a similar problem. The problem was the fact that my phone was causing incorrect date so that all cookies were empty !!!
After many tries i found that the BasicCookieStore was not adding any cookie, because all cookies were expired for an unknown reason (and only on S2). I had to create my own implementation of CookieStore which ignore the expiration date check. This is a temporary fix, i'm still digging to find a more secure and clean way to solve this.
来源:https://stackoverflow.com/questions/11331225/httpclient-not-storing-cookies-on-galaxy-s2