Android WebView removeCookie

时间:2012-08-12 05:32:33

标签: android cookies webview

Android使用CookieManager来管理cookie。但只有一种方法'removeAllCookies()'来删除cookie,这删除了所有的cookie。现在我想删除仅与指定网址关联的Cookie,并且不会删除其他Cookie。在这种情况下,我该怎么办?

3 个答案:

答案 0 :(得分:4)

我在@ summerxiaqing的回答中做了一些小改动,我查了一下。 (添加了https,将空检查更改为TextUtils.isEmpty,通过参数传递CookieManager)

     public static void clearCookieByUrl(String url, CookieManager pCookieManager, CookieSyncManager pCookieSyncManager) {
        Uri uri = Uri.parse(url);
        String host = uri.getHost();
        clearCookieByUrlInternal(url,pCookieManager,pCookieSyncManager);
        clearCookieByUrlInternal("http://." + host,pCookieManager,pCookieSyncManager);
        clearCookieByUrlInternal("https://." + host,pCookieManager,pCookieSyncManager);
    }

     private static void clearCookieByUrlInternal(String url, CookieManager pCookieManager, CookieSyncManager pCookieSyncManager) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        String cookieString = pCookieManager.getCookie(url);
        Vector<String> cookie = getCookieNamesByUrl(cookieString);
        if (cookie == null || cookie.isEmpty()) {
            return;
        }
        int len = cookie.size();
        for (int i = 0; i < len; i++) {
            pCookieManager.setCookie(url, cookie.get(i) + "=-1");
        }
        pCookieSyncManager.sync();
    }

    private static Vector<String> getCookieNamesByUrl(String cookie) {
        if (TextUtils.isEmpty(cookie)) {
            return null;
        }
        String[] cookieField = cookie.split(";");
        int len = cookieField.length;
        for (int i = 0; i < len; i++) {
            cookieField[i] = cookieField[i].trim();
        }
        Vector<String> allCookieField = new Vector<String>();
        for (int i = 0; i < len; i++) {
            if (TextUtils.isEmpty(cookieField[i])) {
                continue;
            }
            if (!cookieField[i].contains("=")) {
                continue;
            }
            String[] singleCookieField = cookieField[i].split("=");
            allCookieField.add(singleCookieField[0]);
        }
        if (allCookieField.isEmpty()) {
            return null;
        }
        return allCookieField;
    }

活动内部

mContext = getApplicationContext();
CookieManager mCookieManager = CookieManager.getInstance();
CookieSyncManager mCookieSyncManager = CookieSyncManager.createInstance(mContext);
clearCookieByUrl("http://example.com", mCookieManager, mCookieSyncManager);

答案 1 :(得分:2)

1,Android不支持此界面。

2,在我们的项目中解决。

sCookieManager = CookieManager.getInstance();

public static void clearCookieByUrl(String url) {

    Uri uri = Uri.parse(url);
    String host = uri.getHost();
    clearCookieByUrlInternal(url);
    clearCookieByUrlInternal("http://." + host);
  }

/**
   * clear cookie by url
   * @param url
   */
  private static void clearCookieByUrlInternal(String url) {
    if (Utils.isStringEmpty(url)) {
      return;
    }
    String cookieString = sCookieManager.getCookie(url);
    Vector<String> cookie = getCookieNamesByUrl(cookieString);
    if (cookie == null || cookie.isEmpty()) {
      return;
    }
    int len = cookie.size();
    for (int i = 0; i < len; i++) {
      sCookieManager.setCookie(url, cookie.get(i) + "=-1");
    }
    sCookieSyncManager.sync();
  }

private static Vector<String> getCookieNamesByUrl(String cookie) {
    if (Utils.isStringEmpty(cookie)) {
      return null;
    }
    String[] cookieField = cookie.split(";");
    int len = cookieField.length;
    for (int i = 0; i < len; i++) {
      cookieField[i] = cookieField[i].trim();
    }
    Vector<String> allCookieField = new Vector<String>();
    for (int i = 0; i < len; i++) {
      if (Utils.isStringEmpty(cookieField[i])) {
        continue;
      }
      if (!cookieField[i].contains("=")) {
        continue;
      }
      String[] singleCookieField = cookieField[i].split("=");
      allCookieField.add(singleCookieField[0]);
    }
    if (allCookieField.isEmpty()) {
      return null;
    }
    return allCookieField;
  }

注意:在某些Android版本中(我没有确切的版本号,我的经验是android 2.3),系统将使用主机作为cookie域,另一方面,系统将使用'。' + host作为cookie域。因此,更好的方法是清楚它们。

答案 2 :(得分:0)

其他两个答案适用于Lollipop之前的版本。在较新的版本中,您应该替换:

sCookieSyncManager.sync();

具有:

sCookieManager.flush();