I'm using a WebView in an Android app. I periodically to be able to reset the WebView http session so that the server app it is accessing will initialise a new session. I can't find how to do this - is this possible ?
Rgds, Kevin.
I think the clear cookie can make session close. More about session
CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
import android.webkit.CookieManager;
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeSessionCookie();
removeSessionCookie() allows to keep other cookies but clear sessions.
There are a couple of things you can do to clear the webview depending exactly what you want to do:
webView.clearCache(true);
webView.clearHistory();
webView.destroy();
This worked for me:
WebStorage.getInstance().deleteAllData()
Related
In KitKat and above, this code works perfectly:
ActivityManager manager = ((ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE));
manager.clearApplicationUserData();
It wipes all of the app settings, any internal databases, webview caches, etc. It's great.
I want to write some code that will do this same thing for users with versions below KitKat.
I've tried:
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear com.example.myapp");
That doesn't work.
Also tried:
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
That doesn't work.
Also tried a bunch of clearCache(), clearHistory(), .setAppCacheEnabled(false), etc on the webview. No luck.
Basically, my biggest problem is the webview seems to still be caching data (ie. if I've logged into a site through the webview, I want to be able to delete that session permanently and log the user out). Nothing I do (except clearApplicationUserData) is able to fix this.
Thanks
If anyone else has this problem I did this to fix it:
CookieSyncManager syncManager = CookieSyncManager.createInstance(activity);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
syncManager.stopSync();
syncManager.sync();
I logged in Instagram using browser. I want to logout from the Instagram. How can i archive programmatically?
You can use below code to clear cookies of browser to make it logout
CookieSyncManager.createInstance(ogout.this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
Hope this helps you
You can use this library for login,for logout just call mApp.resetAccessToken();
https://github.com/thiagolocatelli/android-instagram-oauth
and for webview.use below properties for clearing webview sessions and data.
WebSettings mWebSettings = webview.getSettings();
mWebSettings.setSavePassword(false);
mWebSettings.setSaveFormData(false);
webView.clearCache(true);
webView.clearHistory();
or reset HTTP Session using
android.webkit.CookieManager.getInstance().removeAllCookie();
or
CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
How to clear cookies and cache of webview on Android when not in webview?
This question already has answers here:
Pass cookies from HttpURLConnection (java.net.CookieManager) to WebView (android.webkit.CookieManager)
(4 answers)
Closed 9 years ago.
I have a web service. Normally I give a connection with HttpURLConnection. Here's one I need.
Connect with HttpURLConnection and get session and cookie information from the HttpURLConnection and open WebView.
in short
Input from within the application process whether android. I want to make the rest of the operations in WebView.
Have a look at CookieSyncManager class. With google, I found this:
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mWebView.getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
cookieManager.setCookie("http://xx.xxx.xxx.com","mid="+MySession.GetSession().sessionId+" ; Domain=.xxx.com");
cookieSyncManager.sync();
String cookie = cookieManager.getCookie("http://xx.xxx.xxx.com");
Log.d(LOGTAG, "cookie ------>"+cookie);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new TuWebViewClient());
mWebView.loadUrl("http://xx.xx.xxx.com");
I'm trying to set some cookies on my WebView to open a browser with the same session that I have on my app.
I read a lot of answers but they don't work for me. The only solution I've found is in the loadUrl, hardcode the cookie data in extraHeaders, but as expected this only works for this requests, and doesn't maintain the session.
The code that I have is:
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mWebView.getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
cookieManager.setCookie("http://xx.xxx.example.com","mid="+MySession.GetSession().sessionId+" ; Domain=.example.com");
cookieSyncManager.sync();
String cookie = cookieManager.getCookie("http://xx.xxx.example.com");
Log.d(LOGTAG, "cookie ------>"+cookie);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new TuWebViewClient());
mWebView.loadUrl("http://xx.xx.example.com");
getCookie() returns the correct data, but when I read the cookies from the server, those are empty. What is wrong? Please advise.
Thank you!!!
Solved!!!! the problem is with the webView, I dont know what happend, but If I create the
WebView webView = new WebView(Activity.this);
it works. If I read the webview from activity with findViewById() it doesn't work.
Also if you need to set a list of cookies that you received previously from a website.
All you have to do is use a for-loop to go through and set all of them . It helped me to solve the situation
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mWebView.getContext());
...
cookieSyncManager.sync();
is the cause of problem. You should do like this:
CookieSyncManager.createInstance(mWebView.getContext());
...
CookieSyncManager.getInstance().sync();
And there will be no need to manually create WebView...
CookieManager cookieManager = CookieManager.getInstance();
String cookie = cookieManager.getCookie(url);
cookieManager.removeSessionCookie();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie( url, cookiestring);
removing cookie works,
how do i set the cookie back onto webview and access logged in page again
thanks in advance
You must use the CookieSyncManager. From inside your activity call:
cookieManager.setCookie(url, cookiestring);
CookieSyncManager.createInstance(this).sync();