HttpClient not storing cookies on galaxy s2

陌路散爱 提交于 2019-12-01 12:01:55

问题


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)


回答1:


I had a similar problem. The problem was the fact that my phone was causing incorrect date so that all cookies were empty !!!




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!