Java Code Examples for android.webkit.CookieManager#getCookie()

The following examples show how to use android.webkit.CookieManager#getCookie() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utility.java    From Abelana-Android with Apache License 2.0 6 votes vote down vote up
private static void clearCookiesForDomain(Context context, String domain) {
    // This is to work around a bug where CookieManager may fail to instantiate if CookieSyncManager
    // has never been created.
    CookieSyncManager syncManager = CookieSyncManager.createInstance(context);
    syncManager.sync();

    CookieManager cookieManager = CookieManager.getInstance();

    String cookies = cookieManager.getCookie(domain);
    if (cookies == null) {
        return;
    }

    String[] splitCookies = cookies.split(";");
    for (String cookie : splitCookies) {
        String[] cookieParts = cookie.split("=");
        if (cookieParts.length > 0) {
            String newCookie = cookieParts[0].trim() + "=;expires=Sat, 1 Jan 2000 00:00:01 UTC;";
            cookieManager.setCookie(domain, newCookie);
        }
    }
    cookieManager.removeExpiredCookie();
}
 
Example 2
Source File: Utility.java    From KlyphMessenger with MIT License 6 votes vote down vote up
private static void clearCookiesForDomain(Context context, String domain) {
    // This is to work around a bug where CookieManager may fail to instantiate if CookieSyncManager
    // has never been created.
    CookieSyncManager syncManager = CookieSyncManager.createInstance(context);
    syncManager.sync();

    CookieManager cookieManager = CookieManager.getInstance();

    String cookies = cookieManager.getCookie(domain);
    if (cookies == null) {
        return;
    }

    String[] splitCookies = cookies.split(";");
    for (String cookie : splitCookies) {
        String[] cookieParts = cookie.split("=");
        if (cookieParts.length > 0) {
            String newCookie = cookieParts[0].trim() + "=;expires=Sat, 1 Jan 2000 00:00:01 UTC;";
            cookieManager.setCookie(domain, newCookie);
        }
    }
    cookieManager.removeExpiredCookie();
}
 
Example 3
Source File: Utility.java    From facebook-api-android-maven with Apache License 2.0 6 votes vote down vote up
private static void clearCookiesForDomain(Context context, String domain) {
    // This is to work around a bug where CookieManager may fail to instantiate if CookieSyncManager
    // has never been created.
    CookieSyncManager syncManager = CookieSyncManager.createInstance(context);
    syncManager.sync();

    CookieManager cookieManager = CookieManager.getInstance();

    String cookies = cookieManager.getCookie(domain);
    if (cookies == null) {
        return;
    }

    String[] splitCookies = cookies.split(";");
    for (String cookie : splitCookies) {
        String[] cookieParts = cookie.split("=");
        if (cookieParts.length > 0) {
            String newCookie = cookieParts[0].trim() + "=;expires=Sat, 1 Jan 2000 00:00:01 UTC;";
            cookieManager.setCookie(domain, newCookie);
        }
    }
    cookieManager.removeExpiredCookie();
}
 
Example 4
Source File: Utility.java    From FacebookNewsfeedSample-Android with Apache License 2.0 6 votes vote down vote up
private static void clearCookiesForDomain(Context context, String domain) {
    // This is to work around a bug where CookieManager may fail to instantiate if CookieSyncManager
    // has never been created.
    CookieSyncManager syncManager = CookieSyncManager.createInstance(context);
    syncManager.sync();

    CookieManager cookieManager = CookieManager.getInstance();

    String cookies = cookieManager.getCookie(domain);
    if (cookies == null) {
        return;
    }

    String[] splitCookies = cookies.split(";");
    for (String cookie : splitCookies) {
        String[] cookieParts = cookie.split("=");
        if (cookieParts.length > 0) {
            String newCookie = cookieParts[0].trim() + "=;expires=Sat, 1 Jan 2000 00:00:01 UTC;";
            cookieManager.setCookie(domain, newCookie);
        }
    }
    cookieManager.removeExpiredCookie();
}
 
Example 5
Source File: Utility.java    From kognitivo with Apache License 2.0 6 votes vote down vote up
private static void clearCookiesForDomain(Context context, String domain) {
    // This is to work around a bug where CookieManager may fail to instantiate if
    // CookieSyncManager has never been created.
    CookieSyncManager syncManager = CookieSyncManager.createInstance(context);
    syncManager.sync();

    CookieManager cookieManager = CookieManager.getInstance();

    String cookies = cookieManager.getCookie(domain);
    if (cookies == null) {
        return;
    }

    String[] splitCookies = cookies.split(";");
    for (String cookie : splitCookies) {
        String[] cookieParts = cookie.split("=");
        if (cookieParts.length > 0) {
            String newCookie = cookieParts[0].trim() +
                    "=;expires=Sat, 1 Jan 2000 00:00:01 UTC;";
            cookieManager.setCookie(domain, newCookie);
        }
    }
    cookieManager.removeExpiredCookie();
}
 
Example 6
Source File: SsoWebViewClient.java    From Cirrus_depricated with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onPageFinished (WebView view, String url) {
    Log_OC.d(TAG, "onPageFinished : " + url);
    mLastReloadedUrlAtError = null;
    if (url.startsWith(mTargetUrl)) {
        view.setVisibility(View.GONE);
        CookieManager cookieManager = CookieManager.getInstance();
        final String cookies = cookieManager.getCookie(url);
        //Log_OC.d(TAG, "Cookies: " + cookies);
        if (mListenerHandler != null && mListenerRef != null) {
            // this is good idea because onPageFinished is not running in the UI thread
            mListenerHandler.post(new Runnable() {
                @Override
                public void run() {
                    SsoWebViewClientListener listener = mListenerRef.get();
                    if (listener != null) {
                    	// Send Cookies to the listener
                        listener.onSsoFinished(cookies);
                    }
                }
            });
        }
    } 
}
 
Example 7
Source File: PrebidServerAdapter.java    From prebid-mobile-android with Apache License 2.0 6 votes vote down vote up
private String getExistingCookie() {
    try {
        CookieSyncManager.createInstance(PrebidMobile.getApplicationContext());
        CookieManager cm = CookieManager.getInstance();
        if (cm != null) {
            String wvcookie = cm.getCookie(PrebidServerSettings.COOKIE_DOMAIN);
            if (!TextUtils.isEmpty(wvcookie)) {
                String[] existingCookies = wvcookie.split("; ");
                for (String cookie : existingCookies) {
                    if (cookie != null && cookie.contains(PrebidServerSettings.AN_UUID)) {
                        return cookie;
                    }
                }
            }
        }
    } catch (Exception e) {
    }
    return null;
}
 
Example 8
Source File: Utility.java    From FacebookImageShareIntent with MIT License 6 votes vote down vote up
private static void clearCookiesForDomain(Context context, String domain) {
    // This is to work around a bug where CookieManager may fail to instantiate if CookieSyncManager
    // has never been created.
    CookieSyncManager syncManager = CookieSyncManager.createInstance(context);
    syncManager.sync();

    CookieManager cookieManager = CookieManager.getInstance();

    String cookies = cookieManager.getCookie(domain);
    if (cookies == null) {
        return;
    }

    String[] splitCookies = cookies.split(";");
    for (String cookie : splitCookies) {
        String[] cookieParts = cookie.split("=");
        if (cookieParts.length > 0) {
            String newCookie = cookieParts[0].trim() + "=;expires=Sat, 1 Jan 2000 00:00:01 UTC;";
            cookieManager.setCookie(domain, newCookie);
        }
    }
    cookieManager.removeExpiredCookie();
}
 
Example 9
Source File: InjectWebViewClient.java    From cordova-plugin-fastrde-injectview with MIT License 5 votes vote down vote up
public String getCookies(String siteName){
      String CookieValue = null;

      CookieManager cookieManager = CookieManager.getInstance();
      String cookies = cookieManager.getCookie(siteName);
		return cookies;
}
 
Example 10
Source File: InjectWebViewClient.java    From cordova-plugin-fastrde-injectview with MIT License 5 votes vote down vote up
public String getCookie(String siteName,String CookieName){
    String CookieValue = null;

    CookieManager cookieManager = CookieManager.getInstance();
    String cookies = cookieManager.getCookie(siteName);
    String[] temp=cookies.split(";");
    for (String ar1 : temp ){
        if(ar1.contains(CookieName)){
            String[] temp1=ar1.split("=");
            CookieValue = temp1[1];
        }
    }
    return CookieValue;
}
 
Example 11
Source File: WebviewUtil.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
public static String getCookie() {
    try {
        CookieManager cm = CookieManager.getInstance();
        if (cm == null) {
            Clog.i(Clog.httpRespLogTag, "Unable to find a CookieManager");
            return null;
        }
        return cm.getCookie(Settings.getBaseUrl());
    }catch (Exception e){
        // Catches PackageManager$NameNotFoundException for webview
        Clog.e(Clog.httpRespLogTag, "Unable to find a CookieManager - Exception: "+e.getMessage());
    }
    return null;
}
 
Example 12
Source File: WebviewUtil.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
private static String getExistingANUUID() {
    CookieManager cm = CookieManager.getInstance();
    if (cm != null) {
        String wvcookie = cm.getCookie(Settings.getCookieDomain());
        if (!TextUtils.isEmpty(wvcookie)) {
            String[] existingCookies = wvcookie.split("; ");
            for (String cookie : existingCookies) {
                if (cookie != null && cookie.contains(Settings.AN_UUID)) {
                    return cookie;
                }
            }
        }
    }
    return null;
}
 
Example 13
Source File: ProgressWebView.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 设置当前登录用户的cookie信息
 * @param context
 * @return
 */
public void webViewSetCookie(Context context, String url) {
    //设置cookie
    String domain = StringUtil.getTopDomain(url);
    XLog.debug("domain:"+domain);
    String cookie = "x-token="+ O2SDKManager.Companion.instance().getZToken();
    XLog.debug("cookie:"+cookie);
    String host = APIAddressHelper.Companion.instance().getWebViewHost();
    XLog.debug("host:"+host);
    String cookieStr;
    if (StringUtil.isIp(host)){
        cookieStr = cookie;
    }else {
        cookieStr = cookie +"; path=/; domain=."+domain;
    }
    XLog.debug("Set-Cookie:"+cookieStr);

    CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.setCookie(url, cookieStr);
    CookieSyncManager.getInstance().sync();

    String newCookie = cookieManager.getCookie(url);
    if(newCookie != null){
        XLog.debug("Nat: webView.syncCookie.newCookie "+newCookie);
    }
    XLog.debug("mCookieManager is finish");
}
 
Example 14
Source File: Login.java    From NClientV2 with Apache License 2.0 5 votes vote down vote up
public static void clearCookies(Context context,String domain) {
    CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    String cookiestring = cookieManager.getCookie(domain);
    String[] cookies =  cookiestring.split(";");
    for (String cookie : cookies) {
        String[] cookieparts = cookie.split("=");
        cookieManager.setCookie(domain, cookieparts[0].trim() + "=; Expires=Tue, 31 Dec 2019 23:59:59 GMT");
    }
    CookieSyncManager.getInstance().sync();
}
 
Example 15
Source File: UConfigActivity.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDestroy() {
  super.onDestroy();
  webView.destroy();

  // Put cookies back to okhttp cookie store
  CookieManager cookieManager = CookieManager.getInstance();
  String cookiesString = cookieManager.getCookie(url);

  if (cookiesString != null && !cookiesString.isEmpty()) {
    EhCookieStore store = EhApplication.getEhCookieStore(this);
    HttpUrl eUrl = HttpUrl.parse(EhUrl.HOST_E);
    HttpUrl exUrl = HttpUrl.parse(EhUrl.HOST_EX);

    // The cookies saved in the uconfig page should be shared between e and ex
    for (String header : cookiesString.split(";")) {
      Cookie eCookie = Cookie.parse(eUrl, header);
      if (eCookie != null) {
        store.addCookie(longLive(eCookie));
      }

      Cookie exCookie = Cookie.parse(exUrl, header);
      if (exCookie != null) {
        store.addCookie(longLive(exCookie));
      }
    }
  }
}
 
Example 16
Source File: GSIDWebViewActivity.java    From iBeebo with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onComplete(Bundle values) {
    CookieManager cookieManager = CookieManager.getInstance();

    String cookie = cookieManager.getCookie(SeniorUrl.SeniorUrl_SeniorLogin);
    String pubCookie = cookieManager.getCookie(SeniorUrl.SeniorUrl_Public);

    String passPortCookie = cookieManager.getCookie("https://passport.weibo.cn");

    DevLog.printLog("Weibo-CookieStr cookie: ", cookie);
    DevLog.printLog("Weibo-CookieStr pubCookie: ", pubCookie);
    DevLog.printLog("Weibo-CookieStr passPortCookie: ", passPortCookie);

    String uid = "";
    String gsid = "";

    if (!TextUtils.isEmpty(cookie)) {
        String[] cookies = cookie.split("; ");
        for (String string : cookies) {
            String oneLine = Uri.decode(Uri.decode(string));

            if (oneLine.contains("SUB=")) {
                DevLog.printLog("GSID", "" + oneLine);
                gsid = oneLine.split("SUB=")[1];
            }

            if (oneLine.contains("SSOLoginState")) {
                uid = oneLine.split("=")[1];
                DevLog.printLog("GSID-UID", uid);
            }
        }
    }

    if (!TextUtils.isEmpty(uid)) {
        AccountDatabaseManager manager = new AccountDatabaseManager(getApplicationContext());
        manager.updateAccount(AccountTable.ACCOUNT_TABLE, mAccountBean.getUid(), AccountTable.COOKIE, pubCookie);
        manager.updateAccount(AccountTable.ACCOUNT_TABLE, mAccountBean.getUid(), AccountTable.GSID, gsid);
        BeeboApplication.getInstance().updateAccountBean();

        finish();
    } else if (!TextUtils.isEmpty(uid)) {
        Toast.makeText(getApplicationContext(), "请登录昵称是[" + mAccountBean.getUsernick() + "]的微博!", Toast.LENGTH_LONG)
                .show();
        mWebView.loadUrl(SeniorUrl.SeniorUrl_SeniorLogin);
    }
}
 
Example 17
Source File: DefaultSonicRuntimeImpl.java    From AgentWeb with Apache License 2.0 4 votes vote down vote up
@Override
public String getCookie(String url) {
    CookieManager cookieManager = CookieManager.getInstance();
    return cookieManager.getCookie(url);
}
 
Example 18
Source File: JSWebViewActivity.java    From iBeebo with GNU General Public License v3.0 4 votes vote down vote up
@Override
    public void onComplete(Bundle values) {
        // TODO Auto-generated method stub
        CookieManager cookieManager = CookieManager.getInstance();

        String cookie = cookieManager.getCookie(url);

//         setWeiboCookie(CookieStr);
        String uid = "";
        String uname = "";
        AccountDatabaseManager manager = new AccountDatabaseManager(getApplicationContext());
        if (true) {
            String[] cookies = cookie.split("; ");
            for (String string : cookies) {
                // Log.d("Weibo-Cookie", "" + Uri.decode(Uri.decode(string)));
                String oneLine = Uri.decode(Uri.decode(string));
                String uidtmp = PatternUtils.macthUID(oneLine);
                if (!TextUtils.isEmpty(uidtmp)) {
                    uid = uidtmp;
                }
                uname = PatternUtils.macthUname(oneLine);
                // Log.d("Weibo-Cookie", "" + uid);
                // Log.d("Weibo-Cookie", "" + uname);
                // Log.d("Weibo-Cookie", "in db : uid = " + mAccountBean.getUid());

                if (!TextUtils.isEmpty(uname)) {
                    manager.updateAccount(AccountTable.ACCOUNT_TABLE, uid, AccountTable.USER_NAME, uname);
                }
            }
        }

        Log.d("Weibo-Cookie", "after for : " + uid);
        if (uid.equals(mAccountBean.getUid())) {
            manager.updateAccount(AccountTable.ACCOUNT_TABLE, uid, AccountTable.COOKIE, cookie);
            BeeboApplication.getInstance().updateAccountBean();
            finish();
        } else if (!TextUtils.isEmpty(uid)) {
            Toast.makeText(getApplicationContext(), "请登录昵称是[" + mAccountBean.getUsernick() + "]的微博!", Toast.LENGTH_LONG)
                    .show();
            mWebView.loadUrl(url);
        }
    }
 
Example 19
Source File: WebViewActivity.java    From iBeebo with GNU General Public License v3.0 4 votes vote down vote up
@Override
    public void onComplete(Bundle values) {
        // TODO Auto-generated method stub
        CookieManager cookieManager = CookieManager.getInstance();

        String cookie = cookieManager.getCookie(SeniorUrl.SeniorUrl_SeniorLogin);

//         setWeiboCookie(CookieStr);
        String uid = "";
        String uname = "";
        AccountDatabaseManager manager = new AccountDatabaseManager(getApplicationContext());
        if (true) {
            String[] cookies = cookie.split("; ");
            for (String string : cookies) {
                // Log.d("Weibo-Cookie", "" + Uri.decode(Uri.decode(string)));
                String oneLine = Uri.decode(Uri.decode(string));
                String uidtmp = PatternUtils.macthUID(oneLine);
                if (!TextUtils.isEmpty(uidtmp)) {
                    uid = uidtmp;
                }
                uname = PatternUtils.macthUname(oneLine);
                // Log.d("Weibo-Cookie", "" + uid);
                // Log.d("Weibo-Cookie", "" + uname);
                // Log.d("Weibo-Cookie", "in db : uid = " + mAccountBean.getUid());

                if (!TextUtils.isEmpty(uname)) {
                    manager.updateAccount(AccountTable.ACCOUNT_TABLE, uid, AccountTable.USER_NAME, uname);
                }
            }
        }

        Log.d("Weibo-Cookie", "after for : " + uid);
        if (uid.equals(mAccountBean.getUid())) {
            manager.updateAccount(AccountTable.ACCOUNT_TABLE, uid, AccountTable.COOKIE, cookie);
            cookieManager.removeSessionCookie();
            finish();
        } else if (!TextUtils.isEmpty(uid)) {
            Toast.makeText(getApplicationContext(), "请登录昵称是[" + mAccountBean.getUsernick() + "]的微博!", Toast.LENGTH_LONG)
                    .show();
            mWebView.loadUrl(SeniorUrl.SeniorUrl_SeniorLogin);
        }
    }
 
Example 20
Source File: JSAutoLogin.java    From iBeebo with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {

    DevLog.printLog("JSAutoLogin onPageStarted", url);

    if (url.startsWith(SeniorUrl.SeniorUrl_Public)) {
        view.stopLoading();

        CookieManager cookieManager = CookieManager.getInstance();

        String cookie = cookieManager.getCookie(url);

        String uid = "";
        String uname = "";

        String gsid = "";

        AccountDatabaseManager manager = new AccountDatabaseManager(mContext);
        if (!TextUtils.isEmpty(cookie)) {
            String[] cookies = cookie.split("; ");
            for (String string : cookies) {
                String oneLine = Uri.decode(Uri.decode(string));

                if (oneLine.contains("SUB=")) {
                    DevLog.printLog("GSID", "" + oneLine);
                    gsid = oneLine.split("SUB=")[1];
                }

                String uidtmp = PatternUtils.macthUID(oneLine);
                if (!TextUtils.isEmpty(uidtmp)) {
                    uid = uidtmp;
                }
                uname = PatternUtils.macthUname(oneLine);
                if (!TextUtils.isEmpty(uname)) {
                    manager.updateAccount(AccountTable.ACCOUNT_TABLE, uid, AccountTable.USER_NAME, uname);
                    manager.updateAccount(AccountTable.ACCOUNT_TABLE, uid, AccountTable.GSID, gsid);
                    BeeboApplication.getInstance().updateAccountBean();
                }
            }
        }

        Log.d("Weibo-Cookie", "after for : " + uid);
        if (uid.equals(mAccountBean.getUid())) {
            if (mListener != null) {
                mListener.onAutoLonin(true);
            }
            manager.updateAccount(AccountTable.ACCOUNT_TABLE, uid, AccountTable.COOKIE, cookie);
            BeeboApplication.getInstance().updateAccountBean();
        } else if (!TextUtils.isEmpty(uid)) {
            mWebView.loadUrl(url);
        }

        return;
    }

    super.onPageStarted(view, url, favicon);

}