How to accept cookies when using a webservice - Android? - android

I'm trying to send HTTP Post requests to my webservice. It seems to work, however the cookies are not enabled.
I tried to import android.webkit.CookieManager in my Application and then use this code :
CookieManager cookieManager = new CookieManager();
cookieManager.setAcceptCookie(true);
I get an error telling me that CookieManager cannot be instancied.
Is there a way to allow cookies when sending a POST request ?
Thanks for your help.

Try this for webview cookies
CookieManager.getInstance().setCookie();
http://developer.android.com/reference/android/webkit/CookieManager.html#setCookie(java.lang.String,java.lang.String,android.webkit.ValueCallback

Related

Get session cookie from android webview

I'm currently developing a cordova app with an ADFS authentication through inappbrowser.
This works well so far. But I need to retrieve the session cookie stored in the webview instance and be able to use it in another application with the same mechanism, so the user doesn't need to log in twice, as both apps use the same corporate login.
I've tried recovering the cookie with CookieManager
CookieManager cookieManager = CookieManager.getInstance();
String cookies = cookieManager.getCookie(url);
However, it doesn't list the session cookie. It seems CookieManager doesn't return those cookies without expiration date.
I've been able to successfully terminate the session by running
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeSessionCookies(null);
So I'm sure the session cookie is there, but I'm unable to get/manipulate it.

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

Can I access a HTTP cookie in my webview natively from Android?

I'm writing a Cordova app and I would like to access its HTTP secure cookie from a plugin. I want to encrypt / disable it until the user enters a valid pin.
All help is greatly appreciated. Thank you.
The way I would do it would be to enable/disable the cookies for the entire application:
CookieManager mCookieManager = CookieManager.getInstance();
CookieSyncManager.createInstance(this);
mCookieManager.setAcceptCookie(false); //disables cookies for the WebView until the user enters a correct pin
if(getUsersPin()) { //getUsersPin() gets the pin from the user
mCookieManager.setAcceptCookie(true);
}

How to keep session with HttpUrlConnection?

I'm using HttpUrlConnection to perform some http request and I need to keep a session alive.
I read the doc which just say to add
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
but it does not say where.
Should I add this every time I create a HttpUrlConnection and do a request? I can't find any example...
CookieHandler.setDefault() sets the system-wide cookie handler. You should call it once.

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