Keep session between Android WebView's loadUrl - android

There have been a couple of question's about syncing cookies, but none of the solutions described, seem to work for me.
I've got one webView in my Android app. The first time I show it, I use a HTTP POST to log the user in, and the server shows the homepage. Als long as I stay in this webview, the session is retained. But as soon as I do a new loadUrl on the webview, apparanetly the session is lost, and the server wants me to log in again.
I've tried android-share-session-between-webview-and-httpclient. It doesn't seem to work, in my onPageStarted the list of cookies is always empty.
I've also tried to start syncing cookies before the initial loadURL (with POST) of the webView:
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
CookieSyncManager.createInstance(getBaseContext());
CookieSyncManager.getInstance().startSync();
and then in onPageFinished:
CookieSyncManager.getInstance().sync();
This doesn't work either, so now I really don't know what to do. Does anyone have experience with this and can explain to me how to do it?

Related

Login to Android WebView using browser cookies

I'm using Java code on an Android app to login and I receive cookies after I successfully login (using Retrofit).
What I want to do is if the login is successful and I go to a different tab which has a WebView that I'm automatically logged in with my browser cookie. On iOS this works perfectly. On Android I think I have to change something in my code so I'm using the following code to set the cookies in my CookieManager:
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
for(String cookie: response.headers().values("Set-Cookie")){
System.out.println("cookie = " + cookie);
cookieManager.setCookie(Constants.baseURL, cookie);
}
This works fine and if I reload the WebView it seems to have all the right cookies, why am I not logged in in the WebView and does this work in iOS? Do I need to add something else?
May be you have forgotten to use CookieSyncManager.createInstance(..)
And call sync on obtained instance

Android non synchronizing Cookies

I'm developing apps on iOS and Android where I have a web of shopping and when I buy an item, the web gives me a value on the cookie that I get and then I show the number of items that the client has purchased.
The way to take the cookies is:
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
String cookies = cookieManager.getCookie(url);
so, when I have 20 products and I add one, it turns to 21 (obviously) but when I remove one item, it stays on the same number because the cookies don't get updated.
I'm using the same login with Objective C and it updates well.
Am I missing anything on Android?
Thanks in advance.

getting trouble to set cookie android.webkit.CookieManager

i have login to a site through WebView. then i have get cookie which is set by the webview at login time. then i have tried to set the cookie later. please see my code:
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
Log.e("checkPost 1", cookie);
cookieManager.setCookie("https://************", cookie);
Log.e("checkPost 2", cookieManager.getCookie("https://*************"));
CookieSyncManager.getInstance().sync();
Log.e("checkPost 3", cookieManager.getCookie("https://*************"));
in checkPost 1 printed cookie shows that it is fine. but in checkPost 2 and after sync() it by CookieSyncManager.getInstance().sync(); statement, the checkPost 3 shows that most of the cookie is vanished. what is the problem ? i need to set the cookie. but i could not find a way spending about 2 days.
Edit:
in android documentation it says that:
public void setCookie (String url, String value)
Sets a cookie for the given URL. Any existing cookie with the same
host, path and name will be replaced with the new cookie. The cookie
being set must not have expired and must not be a session cookie,
otherwise it will be ignored.
is there any way to force CookieManager set session cookie via setCookie() ?
i think the problem is, may be i am trying to set session cookie
i don't know there is any answer or not for my question. but for my purpose i have use another way to solve my problem. it's a long process, but it works. i have given up the idea to save session cookie in CookieManager (which is not possible by the document of Android Developer site). So, what i have done is: i have converted the session cookie in string and save them in memory. for later use i just take the string from memory and convert it back to session cookie.
Edit:
(For negative voters) I have told in my answer that the process is too long, like 4-5 classes. and this project was my first project in android so the codes are too messy. all the class is full of too much code. so it is impossible to post it here. i have told the process i have used. so why negative vote?
i have converted the session in my own way and build the string to session in reverse way.
if there is any way i would post the answer. it take me about 15~20 days to build the classes and algorithms. so why not you do some Google and find a better way, or just simply build your own algorithm and process like mine. i have told the key part here, you just need to find a way to implement it into code. Thanks

Android WebView HTML with cookies

I am trying to pull HTML data out of a WebView. I've seen this done a thousand times and I've even gotten it to work myself. However, my new project leads to an interesting situation: I need to login to a site through a WebView and then pull the HTML. Obviously, the Socket method doesn't work, because in order for a webpage to be returned you need the cookie for authentication. I've also tried the JavascriptInterface trick, but that didn't work either. I'm thinking the best way to achieve this is to use HttpGet with CookieManager? Does anyone know how I can get raw HTML code from a WebView with an auth cookie? Thanks!
EDIT: I did some JS injection and didn't see any cookies... so it might not be a cookie issue? But the links that you get redirected to are generic, like mainPage?a=1 and infoPage. You cannot simply copy/paste the links into another browser you have to be logged in to view these links. For those of you who are web experts, you may know an easy solution.
WebView isn't really meant for getting HTML for programmatic use, the idea is that it's just a direct window to a URL for user interaction.
To get what you want, you can use a java.net.HttpURLConnection with a CookieManager, it's worked fine for me on Android and it's suggested in the Android SDK docs:
// in an activity's onCreate:
cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
// later on
void getAPage(URL url) {
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
System.out.println("hello from huc, response code is: " + huc.getResponseCode());
// huc.getInputStream() gives you the content.
}
The CookieManager will persist all cookies through the lifetime of the application without you having to do anything extra. You can make posts with HttpURLConnection too.

Android: Delete cookies in-app // How to get the browser context

I'm failing at the moment in letting my app delete the browser cookies or some specific cookies.
The way I found was to implement the following:
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
But nothing is happening. An with the method hasCookie() is get false as return. Is there no way for deleting a cookie from inside my app?
Thanks for your help!
You can use CookieSyncManager to delete cookies from your own WebView widget(s).
You cannot delete cookies from third-party applications that use WebView or other Web rendering engines.

Categories

Resources