Android 4.4 KitKat not receiving cookies

后端 未结 2 506
悲&欢浪女
悲&欢浪女 2021-01-27 18:04

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,

2条回答
  •  再見小時候
    2021-01-27 19:02

    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();
    

提交回复
热议问题