Example usage for android.webkit CookieManager getCookie

List of usage examples for android.webkit CookieManager getCookie

Introduction

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

Prototype

@SystemApi
public synchronized String getCookie(WebAddress uri) 

Source Link

Document

Gets cookie(s) for a given uri so that it can be set to "cookie:" in http request header.

Usage

From source file:Main.java

public static String getCookie(String siteName, String cookieName) {
    String cookieValue = null;// www  . ja  va  2  s. c  om

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

From source file:Main.java

public static String getCookieValue(Context context, String url, String key) {
    CookieSyncManager csm = CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    csm.sync();/*from  w w  w.  j  av  a 2  s  .c o  m*/
    String cookieStr = cookieManager.getCookie(url);
    System.out.println("========cookied:" + cookieStr);
    String[] strs = cookieStr.split(";");
    String value = null;
    for (String string : strs) {
        if (string.trim().startsWith(key)) {
            value = string.substring(string.indexOf("=") + 1);
            break;
        }
    }
    return value;
}

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 w w  w . j a va2 s . c  o  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.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 ww  . j  a v a 2 s  .  co  m
        //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 a  va  2s . c  o  m
        //new ProfileFetchTask(app).execute();
    }
}

From source file:biz.varkon.shelvesom.util.CookieStore.java

public String getCookie(String url) {
    final CookieManager cookieManager = CookieManager.getInstance();
    String cookie = cookieManager.getCookie(url);

    if (cookie == null || cookie.length() == 0) {
        final HttpHead head = new HttpHead(url);
        HttpEntity entity = null;// w w w  .java2  s  .c o m
        try {
            final HttpResponse response = HttpManager.execute(head);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                entity = response.getEntity();

                final Header[] cookies = response.getHeaders("set-cookie");
                for (Header cooky : cookies) {
                    cookieManager.setCookie(url, cooky.getValue());
                }

                CookieSyncManager.getInstance().sync();
                cookie = cookieManager.getCookie(url);
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "Could not retrieve cookie", e);
        } finally {
            if (entity != null) {
                try {
                    entity.consumeContent();
                } catch (IOException e) {
                    Log.e(LOG_TAG, "Could not retrieve cookie", e);
                }
            }
        }
    }

    return cookie;
}

From source file:org.andrico.andjax.http.HttpMessageFactory.java

/**
 * Create a new http uri request and submit for execution. Promote this to
 * public when I need to actually instantiate an HttpMessage with these
 * parameters.// w  ww .  j ava 2 s .  c  om
 * 
 * @param url The url that this request will access.
 * @param multipartEntity The MultipartEntity to send with the request.
 * @return
 * @throws URISyntaxException
 */
public HttpMessage createFromParts(String url, MultipartEntity multipartEntity) throws URISyntaxException {
    HttpMessage method;
    if (multipartEntity == null) {
        Log.d("HttpMessageFactory", "Using HttpGet");
        method = new HttpGet(url);
    } else {
        Log.d("HttpMessageFactory", "Using HttpPost");
        method = new HttpPost(url);
        ((HttpPost) method).setEntity(multipartEntity);
    }

    if (mCookieDomain != null) {
        final CookieManager cookieManager = CookieManager.getInstance();
        method.addHeader("Cookie", cookieManager.getCookie(this.mCookieDomain));
    }
    ;
    return method;
}

From source file:com.company.millenium.iwannask.RegistrationIntentService.java

public String getCookie(String siteName, String CookieName) {
    String CookieValue = null;// w  w w.ja va  2  s  .  c om

    android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
    String cookies = cookieManager.getCookie(siteName);
    if (cookies != null) {
        String[] temp = cookies.split(";");
        for (String ar1 : temp) {
            if (ar1.contains(CookieName)) {
                String[] temp1 = ar1.split("=");
                return temp1[1];
            }
        }
    }
    return null;
}

From source file:com.drisoftie.cwdroid.frag.FragBoard.java

/**
 * Most actions are created here.//  ww w . ja  v  a 2  s  . c  o m
 */
@SuppressWarnings("unchecked")
private void initActions() {
    actionInit = new ActionBuilder().with().reg(IGenericAction.class, RegActionMethod.NONE)
            .pack(new AndroidAction<View, Void, Void, Void, Void>(null, null) {
                @Override
                public Object onActionPrepare(String methodName, Object[] methodArgs, Void tag1, Void tag2,
                        Object[] additionalTags) {
                    webBoard.loadUrl(getArguments().getString(FragBoard.class.getName()));

                    webBoard.getSettings().setUseWideViewPort(false);
                    webBoard.getSettings().setJavaScriptEnabled(true);

                    CookieManager cm = CookieManager.getInstance();
                    if (!CwApp.domain().isUserLoggedIn()) {
                        cm.removeAllCookie();
                    } else {
                        CwUser user = CwApp.domain().getLoggedInUser();
                        String cookie = cm.getCookie(getString(R.string.api_domain));
                        if (StringUtils.contains(cookie, getString(R.string.cw_cookie_userid))
                                && StringUtils.contains(cookie,
                                        getString(R.string.cw_cookie_userid) + "=" + user.getId())
                                && cookie.contains(getString(R.string.cw_cookie_pw) + "=" + CredentialUtils
                                        .deobfuscateFromBase64(user.getKeyData(), user.getPwHash()))) {
                        } else {
                            cm.removeAllCookie();
                            cm.setCookie(getString(R.string.api_domain),
                                    getString(R.string.cw_cookie_userid) + "=" + user.getId());
                            cm.setCookie(getString(R.string.api_domain),
                                    getString(R.string.cw_cookie_pw) + "=" + CredentialUtils
                                            .deobfuscateFromBase64(user.getKeyData(), user.getPwHash()));
                        }
                    }
                    skipWorkThreadOnce();
                    return null;
                }

                @Override
                public Void onActionDoWork(String methodName, Object[] methodArgs, Void tag1, Void tag2,
                        Object[] additionalTags) {
                    return null;
                }

                @Override
                public void onActionAfterWork(String methodName, Object[] methodArgs, Void result, Void tag1,
                        Void tag2, Object[] additionalTags) {
                }
            });

    actionRefresh = new ActionBuilder().with(swipeBoard)
            .reg(SwipeRefreshLayout.OnRefreshListener.class, "setOnRefreshListener")
            .reg(IGenericAction.class, RegActionMethod.NONE)
            .pack(new AndroidAction<View, Void, Void, Void, Void>(null, null) {
                @Override
                public Object onActionPrepare(String methodName, Object[] methodArgs, Void tag1, Void tag2,
                        Object[] additionalTags) {
                    webBoard.loadUrl(getArguments().getString(FragShoutbox.class.getName()));
                    skipWorkThreadOnce();
                    return null;
                }

                @Override
                public Void onActionDoWork(String methodName, Object[] methodArgs, Void tag1, Void tag2,
                        Object[] additionalTags) {
                    return null;
                }

                @Override
                public void onActionAfterWork(String methodName, Object[] methodArgs, Void result, Void tag1,
                        Void tag2, Object[] additionalTags) {
                }
            });
}

From source file:com.eTilbudsavis.etasdk.SessionManager.java

private void postSession(final Listener<JSONObject> l) {

    Map<String, Object> args = new HashMap<String, Object>();

    args.put(Param.TOKEN_TTL, TTL);//from  www .j av  a  2  s  .c  om
    args.put(Param.API_KEY, mEta.getApiKey());

    CookieSyncManager.createInstance(mEta.getContext());
    CookieManager cm = CookieManager.getInstance();
    String cookieString = cm.getCookie(ETA_COOKIE_DOMAIN);

    if (cookieString != null) {

        // No session yet, check cookies for old token
        String authId = null;
        String authTime = null;
        String authHash = null;

        String[] cookies = cookieString.split(";");
        for (String cookie : cookies) {

            String[] keyValue = cookie.split("=");
            String key = keyValue[0].trim();
            String value = keyValue[1];

            if (value.equals("")) {
                continue;
            }

            if (key.equals(COOKIE_AUTH_ID)) {
                authId = value;
            } else if (key.equals(COOKIE_AUTH_HASH)) {
                authHash = value;
            } else if (key.equals(COOKIE_AUTH_TIME)) {
                authTime = value;
            }

        }

        // If all three fields are set, then try to migrate
        if (authId != null && authHash != null && authTime != null) {
            args.put(Param.V1_AUTH_ID, authId);
            args.put(Param.V1_AUTH_HASH, authHash);
            args.put(Param.V1_AUTH_TIME, authTime);
        }

        // Clear all cookie data, just to make sure
        cm.removeAllCookie();

    }

    JsonObjectRequest req = new JsonObjectRequest(Method.POST, Endpoint.SESSIONS, new JSONObject(args),
            getSessionListener(l));
    addRequest(req);

}