Impossible to update/remove specific cookie for android webview

天涯浪子 提交于 2019-12-13 03:22:46

问题


I am trying to delete a specific cookie for a domain and seems impossible.
In a web view in onPageFinished I can see the cookie and the relevant URL.
I am using the following code to delete the cookie:


    CookieManager cm = CookieManager.getInstance();
    final String url = "https://server.com/foo/index.html?bar=1"; // this is the URL is see in the `onPageFinished`
    String cookiesString = cm.getCookie(url);  
    String [] cookies = cookiesString.split(";");
    for(String cookie:cookies) {
         String[] cookieParts = cookie.split("=");
         if(cookieParts.length <= 0) {
              continue;
         }
         if(cookieParts[0].trim().equals("xxxcookiename")) {
              cm.setCookie(url, cookieParts[0] + "=0;");
          }
          else {
              cm.setCookie(url, cookie);
          }
    }
    cm.flush();

What this does is append a `xxxcookiename=0;` in the cookies for the url. It does not replace/update the existing one with the value `=0`. I also have tried with `=;` and it does not replace it either. I also tried with `https://server.com/foo/index.html` and `https://server.com/foo`.

Basically if I call cm.getCookie(url); right after I flush I get the same cookie string as earlier with xxxcookiename=0; appended and the original xxxcookiename with the old value. What is the issue here?

来源:https://stackoverflow.com/questions/54127921/impossible-to-update-remove-specific-cookie-for-android-webview

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