Example usage for android.webkit CookieManager setCookie

List of usage examples for android.webkit CookieManager setCookie

Introduction

In this page you can find the example usage for android.webkit CookieManager setCookie.

Prototype

public abstract void setCookie(String url, String value);

Source Link

Document

Sets a cookie for the given URL.

Usage

From source file:Main.java

public static void setCookie(Context context, String url) {
    FileInputStream in = null;//from  w w w.j  a  v a 2  s  . c om
    try {
        in = context.openFileInput(TAXICOOKIE_FILE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    if (in == null) {
        Log.w(TAG, "saveCookie: Cannot open file: " + TAXICOOKIE_FILE);
    }

    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String cookieStr = null;
    try {
        cookieStr = reader.readLine();
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Log.d(TAG, "cookieStr: " + cookieStr);
    if (cookieStr == null) {
        return;
    }

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

From source file:Main.java

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();//from  www.ja  va2 s  . co  m

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

From source file:com.nguyenmp.gauchodroid.login.LoginFragment.java

public static void setCookies(Context context, CookieStore cookies) {
    PersistentCookieStore store = new PersistentCookieStore(context);
    store.clear();/*  w w  w. ja v  a 2s.  co  m*/

    CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie();
    cookieManager.setAcceptCookie(true);

    if (cookies != null) {
        for (Cookie cookie : cookies.getCookies()) {
            store.addCookie(cookie);
            cookieManager.setCookie(cookie.getDomain(), cookie.getName() + "=" + cookie.getValue());
        }
    }

    CookieSyncManager.getInstance().sync();
}

From source file:com.jaspersoft.android.jaspermobile.cookie.LegacyCookieManager.java

@Override
public void semanticConfiguration(String targetDomain) {
    CookieSyncManager.createInstance(getContext());

    final CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeSessionCookie();
    cookieManager.setCookie(targetDomain, StringUtils.join(getCookieStore(), ";"));
    CookieSyncManager.getInstance().sync();
}

From source file:org.forgerock.openam.mobile.example.oauth2.activities.webview.AuthorizeWebClient.java

/**
 * Used to append our authentication cookie to the authorization request
 *
 * @param name name of the cookie//from w  w  w. j  a v a2s .c o  m
 * @param value value of the cookie
 * @param domain domain in which the cookie applies
 * @param url url to register the cookie against in the cookie manager
 */
public void insertCookie(String name, String value, String domain, String url) {
    StringBuilder cookie = new StringBuilder(name);
    cookie.append("=").append(value);
    cookie.append("; domain=").append(domain);
    cookie.append("; path=/");

    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.setCookie(url, cookie.toString());
}

From source file:com.jaspersoft.android.jaspermobile.cookie.LollipopCookieManager.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override/*w  ww .  j  av  a  2s  . c o  m*/
protected void semanticConfiguration(final String targetDomain) {
    final CookieManager cookieManager = CookieManager.getInstance();

    cookieManager.removeSessionCookies(new ValueCallback<Boolean>() {
        @Override
        public void onReceiveValue(Boolean value) {
            cookieManager.setCookie(targetDomain, StringUtils.join(getCookieStore(), ";"));
            CookieManager.getInstance().flush();
        }
    });
}

From source file:com.github.dfa.diaspora_android.web.CustomWebViewClient.java

public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);

    final CookieManager cookieManager = app.getCookieManager();
    String cookies = cookieManager.getCookie(url);
    //Log.d(this, "All the cookies in a string:" + cookies);

    if (cookies != null) {
        cookieManager.setCookie(url, cookies);
        cookieManager.setCookie(app.getSettings().getPod().getPodUrl().getBaseUrl(), cookies);
        //for (String c : cookies.split(";")) {
        //AppLog.d(this, "Cookie: " + c.split("=")[0] + " Value:" + c.split("=")[1]);
        //}//  w  w  w  .  ja  va  2s .c  om
        //new ProfileFetchTask(app).execute();
    }
}

From source file:com.github.dfa.diaspora_android.webview.CustomWebViewClient.java

public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);

    final CookieManager cookieManager = app.getCookieManager();
    String cookies = cookieManager.getCookie(url);
    //Log.d(this, "All the cookies in a string:" + cookies);

    if (cookies != null) {
        cookieManager.setCookie(url, cookies);
        cookieManager.setCookie("https://" + app.getSettings().getPodDomain(), cookies);
        //for (String c : cookies.split(";")) {
        //AppLog.d(this, "Cookie: " + c.split("=")[0] + " Value:" + c.split("=")[1]);
        //}//  ww  w .  j  av  a  2  s  .com
        //new ProfileFetchTask(app).execute();
    }
}

From source file:com.hua.nowid.activity.WebViewFragment.java

public void updateSecureCookie() {
    CookieSyncManager.createInstance(this.getActivity().getApplicationContext());
    CookieManager cookieManager = CookieManager.getInstance();
    String cookieString = "NOWSESSIONID=" + NowIDLoginStatus.getInstance().getSecureCookie();
    cookieManager.removeAllCookie();//w  w  w. j av  a  2 s .c  om
    cookieManager.setCookie(".now.com", cookieString);
    Log.v("WebViewFragment", cookieString);
    CookieSyncManager.getInstance().sync();
}

From source file:net.ecfirm.ec.ec1.net.EcNet.java

protected void setCookie() throws Exception {
    Cookie cookie = null;/*from  w  w  w.ja va 2s . com*/
    if ((EcNetHelper.cookies != null) && (EcNetHelper.cookies.size() > 0))
        cookie = EcNetHelper.cookies.get(0);
    CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    if (cookie != null) {
        cookieManager.removeSessionCookie();
        String cookieStr = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();
        cookieManager.setCookie(net.getProtocol() + "://" + net.getHost(), cookieStr);
        CookieSyncManager.getInstance().sync();
    }
}