I have an activity that lets you sign in to a page. In the next activity it should display a webpage based on the cookie if the login was successful. The cookie is retrived and I try to put it on the webView with the following code:
Cookie sessionCookie = LoginWebView.cookie;
CookieSyncManager.createInstance(webview.this);
CookieManager cookieManager = CookieManager.getInstance();
if (sessionCookie != null) {
cookieManager.removeSessionCookie();
String cookieString = sessionCookie.getName() + "=" + sessionCookie.getValue() + "; domain=" + sessionCookie.getDomain();
Log.v(TAG, "COOKIE SYNC: " + cookieString);
cookieManager.setCookie(domain, cookieString);
CookieSyncManager.getInstance().sync();
}
webView.setWebViewClient(new MyWebViewClient ());
webView.loadUrl("http://a_page.com/getpageiphone.aspx?p=home");
This is losely based on code from other questions here on StackOverflow, but when I load the web-page it does not seem to work. It seems as there is something very wrong with my code but I can't see where and I'm starting to think I'm doing something very wrong.
The first cookies are thought to be test cakes bakers used to test the oven temperature. They date back as early as 7th Century A.D. Persia which is now Iran. They were one of the first countries to grow and harvest sugar cane.
The word “cookie” originates from the Dutch word 'Koekje' meaning 'little cake'. These little cakes were originally made to test the temperature of an oven before baking a real cake! Much like cake, cookies are made from a soft, thick dough and are denser than an English biscuit.
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.
by using SystemClock.sleep(500);
, you just gave system to finish removeSessionCookie() first
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.
First of all, make sure to read the header values that your website gives you. You can determine this via Wireshark.
Secondly, when you know how the cookie look like (based from your Wireshark values), you should try to achieve the same values in your code. Make sure to debug the cookie's value(s) and make adjustments on the cookie itself if it doesn't match.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With