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...
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 have cookies,want to send the cookie to WebView,how i can do?
i use JSOUP to get cookies from login page and i use the follow code but not working
webview.LoadUrl(URL, Cookies)
I had to do something like this a couple of weeks ago. I ditched the webview though and used a recyclerview for the info because of data and ram consumption.
res = Jsoup.connect(url)
.data("user", user, "passwrd", pass)
.method(Connection.Method.POST)
.execute();
String sessionId = res.cookie("PHPSESSID");
The code up there is actually from Jsoup's creator himself on how to store cookies. Then its a matter of setting the cookiemanager to these cookies.
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(url,String.format("%s=%s",
"PHPSESSID", sessionId));
Then I just loaded the webview. I think a really important line here is formatting the string in the second parameter of the setCookie() method to match how the cookie is actually a Map with a key and value of PHPSESSID and the cookies characters. PHPSESSID is the id name that shows up for me in the cookie.
How do I enable cookies in a webview?
I tried to use
CookieManager.getInstance().setAcceptCookie(true);
just before calling WebView.loadUrl() and it doesn't work as I get an HTML page error from a website saying cookies need to be enabled.
How does cookieManager know which webview to enable cookies?
Say if I had an activity with two webviews in the screen and I only wanted one of those webviews to enable cookies, how is that possible using a CookieManager?
I feel like I am missing something. I could not find a method like webView.setCookieManager or Cookiemanager.setWebView(webview).
You should consider that
CookieManager.getInstance().setAcceptCookie(true);
doesn't work from lollipop(API21) and above. You should check and use appropriate function for that case:
if (android.os.Build.VERSION.SDK_INT >= 21) {
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);
} else {
CookieManager.getInstance().setAcceptCookie(true);
}
CookieManager.getInstance() is the CookieManager instance for your entire application.
Hence, you enable or disable cookies for all the webviews in your application.
Normally it should work if your webview is already initialized:
http://developer.android.com/reference/android/webkit/CookieManager.html#getInstance()
Maybe you call CookieManager.getInstance().setAcceptCookie(true); before you initialize your webview and this is the problem?
I've found a few answers to this question here on SO, but I'm not having success implementing them so hoped that someone could check over my code and tell me where i'm going wrong.
I have verified that my cookie is being set from the DefaultHttpClient and am writing it out with Log.d as you can see in my code snippet below. It's all there, it's just not getting set for the WebView which is called at the bottom of this code segment. The cookie being passed down is a session cookie from my Asp.net mvc project. Any ideas why it's not keeping me logged in when the WebView hits the site? Don't let the facebook wording throw you off, that I all have working, this cookie is for my own server that is passing down a session cookie to keep me logged into my own site (based on my facebook stuff).
Cookie sessionCookie = FacebookLoginService.cookie;
Log.d("intialwebview",sessionCookie.toString());
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
if (sessionCookie != null) {
cookieManager.removeSessionCookie();
String cookieString = sessionCookie.getName() + "=" + sessionCookie.getValue() + "; domain=" + sessionCookie.getDomain();
Log.d("intialwebview",cookieString);
cookieManager.setCookie(FacebookLoginService.cookie.getDomain(), cookieString);
CookieSyncManager.getInstance().sync();
}
//end cookie section
int userId = prefs.getInt(C.USER_ID, 0);
webView = (WebView) findViewById(R.id.web_view);
webView.getSettings().setUserAgentString(webView.getSettings().getUserAgentString() + "(Rambo)");
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.loadUrl(C.BaseUrl + "home/firstpage");
EDIT: I also tried creating the web view in code with:
WebView webView = new WebView(this);
same result. Cookie isn't being sent to the webview.
finally got this sorted by using an Asynctask and running a sleep in the background.
You have used this line -
if (sessionCookie != null) {
cookieManager.removeSessionCookie();
}
To ensure you receive new cookie everytime.
Seems like you have gone through same issue
as I faced, check below link -
removeSessionCookie() issue of android, (code.google.com)
it says that removeSessionCookie() is implemented in a thread, so whenever it is
called; a thread starts and after your setCookie(url, cookieString); is called,
it removes the new cookie you just set.
So for some devices it works well as removeSessionCookie() is already executed,
while, for some, it remove the cookie, and we get that problem.
I suggest you remove this removeSessionCookie(); as you are setting only one cookie, so it won't conflict with other cookies. Your code will work seamlessly.
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()