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

The following examples show how to use android.webkit.CookieManager#removeSessionCookie() . 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: BaseWebView.java    From dcs-sdk-java with Apache License 2.0 6 votes vote down vote up
private void setCookie(Context context, String domain, String sessionCookie) {
    CookieSyncManager.createInstance(context);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    if (sessionCookie != null) {
        // delete old cookies
        cookieManager.removeSessionCookie();
    }
    try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    cookieManager.setCookie(domain, sessionCookie);

    CookieSyncManager.createInstance(context);
    CookieSyncManager.getInstance().sync();
}
 
Example 2
Source File: ProWebView.java    From prowebview with Apache License 2.0 6 votes vote down vote up
/**
 * Clear the cookies
 */
@SuppressWarnings("deprecation")
public void clearCookies() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        CookieManager.getInstance().removeAllCookies(null);
        CookieManager.getInstance().flush();
    } else {
        CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(getContext());
        cookieSyncMngr.startSync();
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncMngr.stopSync();
        cookieSyncMngr.sync();
    }
}
 
Example 3
Source File: HttpTask.java    From letv with Apache License 2.0 6 votes vote down vote up
protected void sendResponseMessage(HttpResponse response) {
    super.sendResponseMessage(response);
    Header[] headers = response.getHeaders("Set-Cookie");
    if (headers != null && headers.length > 0) {
        CookieSyncManager.createInstance(this.val$context).sync();
        CookieManager instance = CookieManager.getInstance();
        instance.setAcceptCookie(true);
        instance.removeSessionCookie();
        String mm = "";
        for (Header header : headers) {
            String[] split = header.toString().split("Set-Cookie:");
            EALogger.i("正式登录", "split[1]===>" + split[1]);
            instance.setCookie(Constants.THIRDLOGIN, split[1]);
            int index = split[1].indexOf(";");
            if (TextUtils.isEmpty(mm)) {
                mm = split[1].substring(index + 1);
                EALogger.i("正式登录", "mm===>" + mm);
            }
        }
        EALogger.i("正式登录", "split[1222]===>COOKIE_DEVICE_ID=" + LemallPlatform.getInstance().uuid + ";" + mm);
        instance.setCookie(Constants.THIRDLOGIN, "COOKIE_DEVICE_ID=" + LemallPlatform.getInstance().uuid + ";" + mm);
        instance.setCookie(Constants.THIRDLOGIN, "COOKIE_APP_ID=" + LemallPlatform.getInstance().getmAppInfo().getId() + ";" + mm);
        CookieSyncManager.getInstance().sync();
        this.val$iLetvBrideg.reLoadWebUrl();
    }
}
 
Example 4
Source File: HttpTask.java    From letv with Apache License 2.0 6 votes vote down vote up
protected void sendResponseMessage(HttpResponse response) {
    super.sendResponseMessage(response);
    Header[] headers = response.getHeaders("Set-Cookie");
    if (headers != null && headers.length > 0) {
        CookieSyncManager.createInstance(LemallPlatform.getInstance().getContext()).sync();
        CookieManager instance = CookieManager.getInstance();
        instance.setAcceptCookie(true);
        instance.removeSessionCookie();
        String mm = "";
        for (Header header : headers) {
            String[] split = header.toString().split("Set-Cookie:");
            EALogger.i("游客登录", "split[1]===>" + split[1]);
            instance.setCookie(Constants.GUESTLOGIN, split[1]);
            int index = split[1].indexOf(";");
            if (TextUtils.isEmpty(mm)) {
                mm = split[1].substring(index + 1);
                EALogger.i("正式登录", "mm===>" + mm);
            }
        }
        EALogger.i("游客登录", "split[11]===>COOKIE_DEVICE_ID=" + LemallPlatform.getInstance().uuid + ";" + mm);
        instance.setCookie(Constants.GUESTLOGIN, "COOKIE_DEVICE_ID=" + LemallPlatform.getInstance().uuid + ";" + mm);
        instance.setCookie(Constants.THIRDLOGIN, "COOKIE_APP_ID=" + LemallPlatform.getInstance().getmAppInfo().getId() + ";" + mm);
        CookieSyncManager.getInstance().sync();
        this.val$iLetvBrideg.reLoadWebUrl();
    }
}
 
Example 5
Source File: Util.java    From CoolApk-Console with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @see <a href="http://stackoverflow.com/questions/28998241/how-to-clear-cookies-and-cache-of-webview-on-android-when-not-in-webview" />
 * @param context
 */
@SuppressWarnings("deprecation")
public static void clearCookies(Context context)
{

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        CookieManager.getInstance().removeAllCookies(null);
        CookieManager.getInstance().flush();
    } else
    {
        CookieSyncManager cookieSyncMngr= CookieSyncManager.createInstance(context);
        cookieSyncMngr.startSync();
        CookieManager cookieManager= CookieManager.getInstance();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncMngr.stopSync();
        cookieSyncMngr.sync();
    }
}
 
Example 6
Source File: Cookies.java    From DelegateAdapter with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
    public static void clearCookies(Context context) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            //Log.d(C.TAG, "Using clearCookies code for API >=" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
            CookieManager.getInstance().removeAllCookies(null);
            CookieManager.getInstance().flush();
        } else {
//            Log.d(C.TAG, "Using clearCookies code for API <" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
            CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context);
            cookieSyncMngr.startSync();
            CookieManager cookieManager=CookieManager.getInstance();
            cookieManager.removeAllCookie();
            cookieManager.removeSessionCookie();
            cookieSyncMngr.stopSync();
            cookieSyncMngr.sync();
        }
    }
 
Example 7
Source File: WebViewSignInScene.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
public View onCreateView2(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Context context = getContext2();
    AssertUtils.assertNotNull(context);

    EhUtils.signOut(context);

    // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
    CookieManager cookieManager = CookieManager.getInstance();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        cookieManager.flush();
        cookieManager.removeAllCookies(null);
        cookieManager.removeSessionCookies(null);
    } else {
        CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context);
        cookieSyncManager.startSync();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncManager.stopSync();
    }

    mWebView = new WebView(context);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new LoginWebViewClient());
    mWebView.loadUrl(EhUrl.URL_SIGN_IN);
    return mWebView;
}
 
Example 8
Source File: WebScraper.java    From VehicleInfoOCR with GNU General Public License v3.0 5 votes vote down vote up
public void clearCookies(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
        CookieManager.getInstance().removeAllCookies(null);
        CookieManager.getInstance().flush();
    }else{
        CookieSyncManager cookieSync = CookieSyncManager.createInstance(context);
        cookieSync.startSync();
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSync.stopSync();
        cookieSync.sync();
    }
}
 
Example 9
Source File: MyTagsActivity.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
  CookieManager cookieManager = CookieManager.getInstance();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    cookieManager.flush();
    cookieManager.removeAllCookies(null);
    cookieManager.removeSessionCookies(null);
  } else {
    CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
    cookieSyncManager.startSync();
    cookieManager.removeAllCookie();
    cookieManager.removeSessionCookie();
    cookieSyncManager.stopSync();
  }

  // Copy cookies from okhttp cookie store to CookieManager
  url = EhUrl.getMyTagsUrl();
  EhCookieStore store = EhApplication.getEhCookieStore(this);
  for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
    cookieManager.setCookie(url, cookie.toString());
  }

  setContentView(R.layout.activity_my_tags);
  setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
  webView = findViewById(R.id.webview);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.setWebViewClient(new MyTagsWebViewClient());
  webView.setWebChromeClient(new DialogWebChromeClient(this));
  webView.loadUrl(url);
  progress = findViewById(R.id.progress);
}
 
Example 10
Source File: NetworkHelper.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
public static void clearCookies(Context context, String s)
{
    CookieSyncManager.createInstance(context);
    CookieManager cookiemanager = CookieManager.getInstance();
    cookiemanager.setAcceptCookie(true);
    cookiemanager.removeSessionCookie();
    cookiemanager.removeAllCookie();
    CookieSyncManager.getInstance().sync();
}
 
Example 11
Source File: TopSlidWebView.java    From MyBlogDemo with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private void setCookie(String url) {
    URI uri = URI.create(url);

    if (TextUtils.isEmpty(uri.getHost())) {
        return;
    }

    if (Build.VERSION.SDK_INT < 21) {
        CookieSyncManager.createInstance(getContext());
    }

    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.removeSessionCookie();
    // 注入cookies
    List<String> cookies = getCookies();
    for (String cookie : cookies) {
        cookieManager.setCookie(uri.getHost(), cookie);
    }

    if (Build.VERSION.SDK_INT >= 21) {
        cookieManager.flush();
    } else {
        CookieSyncManager.getInstance().sync();
    }
}
 
Example 12
Source File: AuthActivity.java    From redgram-for-reddit with GNU General Public License v3.0 5 votes vote down vote up
private void clearCookies(CookieManager manager){
    if(manager.getCookie(getAuthUrl()) != null){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            manager.removeSessionCookies(value -> {});
        }else{
            manager.removeSessionCookie();
        }
    }
}
 
Example 13
Source File: UConfigActivity.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
  CookieManager cookieManager = CookieManager.getInstance();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    cookieManager.flush();
    cookieManager.removeAllCookies(null);
    cookieManager.removeSessionCookies(null);
  } else {
    CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
    cookieSyncManager.startSync();
    cookieManager.removeAllCookie();
    cookieManager.removeSessionCookie();
    cookieSyncManager.stopSync();
  }

  // Copy cookies from okhttp cookie store to CookieManager
  url = EhUrl.getUConfigUrl();
  EhCookieStore store = EhApplication.getEhCookieStore(this);
  for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
    cookieManager.setCookie(url, cookie.toString());
  }

  setContentView(R.layout.activity_u_config);
  setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
  webView = (WebView) findViewById(R.id.webview);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.setWebViewClient(new UConfigWebViewClient());
  webView.setWebChromeClient(new DialogWebChromeClient(this));
  webView.loadUrl(url);
  progress = (ProgressView) findViewById(R.id.progress);

  Snackbar.make(webView, R.string.apply_tip, Snackbar.LENGTH_LONG).show();
}
 
Example 14
Source File: CookieUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public static void removeWebViewCookie(Context context) {
        syncCookie(context);

        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeSessionCookie();//移除
//        cookieManager.setCookie(url, cookies);//cookies是在HttpClient中获得的cookie
//        CookieSyncManager.getInstance().sync();
        CookieManager.getInstance().removeAllCookie();
    }
 
Example 15
Source File: QuickUtil.java    From quickhybrid-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 给webview设置cookie
 *
 * @param context
 * @param url
 */
public static void setCookies(Context context, String url) {
    if (!TextUtils.isEmpty(url)) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
            CookieSyncManager.createInstance( context);
        }
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.removeSessionCookie();
        // 自动注入cookie,这个在使用cookie作为用户校验时有用
        cookieManager.setCookie(url, "JSESSIONID=" + QuickUtil.getToken());
        CookieSyncManager.getInstance().sync();
    }
}
 
Example 16
Source File: UConfigActivity.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
  CookieManager cookieManager = CookieManager.getInstance();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    cookieManager.flush();
    cookieManager.removeAllCookies(null);
    cookieManager.removeSessionCookies(null);
  } else {
    CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
    cookieSyncManager.startSync();
    cookieManager.removeAllCookie();
    cookieManager.removeSessionCookie();
    cookieSyncManager.stopSync();
  }

  // Copy cookies from okhttp cookie store to CookieManager
  url = EhUrl.getUConfigUrl();
  EhCookieStore store = EhApplication.getEhCookieStore(this);
  for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
    cookieManager.setCookie(url, cookie.toString());
  }

  setContentView(R.layout.activity_u_config);
  setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
  webView = (WebView) findViewById(R.id.webview);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.setWebViewClient(new UConfigWebViewClient());
  webView.setWebChromeClient(new DialogWebChromeClient(this));
  webView.loadUrl(url);
  progress = (ProgressView) findViewById(R.id.progress);

  Snackbar.make(webView, R.string.apply_tip, Snackbar.LENGTH_LONG).show();
}
 
Example 17
Source File: MyTagsActivity.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
  CookieManager cookieManager = CookieManager.getInstance();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    cookieManager.flush();
    cookieManager.removeAllCookies(null);
    cookieManager.removeSessionCookies(null);
  } else {
    CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
    cookieSyncManager.startSync();
    cookieManager.removeAllCookie();
    cookieManager.removeSessionCookie();
    cookieSyncManager.stopSync();
  }

  // Copy cookies from okhttp cookie store to CookieManager
  url = EhUrl.getMyTagsUrl();
  EhCookieStore store = EhApplication.getEhCookieStore(this);
  for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
    cookieManager.setCookie(url, cookie.toString());
  }

  setContentView(R.layout.activity_my_tags);
  setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
  webView = findViewById(R.id.webview);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.setWebViewClient(new MyTagsWebViewClient());
  webView.setWebChromeClient(new DialogWebChromeClient(this));
  webView.loadUrl(url);
  progress = findViewById(R.id.progress);
}
 
Example 18
Source File: WebViewSignInScene.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
public View onCreateView2(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Context context = getContext2();
    AssertUtils.assertNotNull(context);

    EhUtils.signOut(context);

    // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
    CookieManager cookieManager = CookieManager.getInstance();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        cookieManager.flush();
        cookieManager.removeAllCookies(null);
        cookieManager.removeSessionCookies(null);
    } else {
        CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context);
        cookieSyncManager.startSync();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncManager.stopSync();
    }

    mWebView = new WebView(context);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new LoginWebViewClient());
    mWebView.loadUrl(EhUrl.URL_SIGN_IN);
    return mWebView;
}
 
Example 19
Source File: TyWebViewCookie.java    From tysq-android with GNU General Public License v3.0 4 votes vote down vote up
public static void removeCookie() {
    CookieManager cookieManager = getInstance();
    cookieManager.removeSessionCookie();
    sync();
}
 
Example 20
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);
        }
    }