Android CookieManager setCookie doesn't set anything - android

in my application, I'm getting two cookies from an HttpGet request and store them in the CookieManager like this:
//Clear old cookies
CookieManager.getInstance().removeAllCookie();
CookieSyncManager.getInstance().sync();
//Save the two cookies: auth token and session info
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
String cookieString = cookie.getName() + "=" + cookie.getValue() + "; Domain=" + cookie.getDomain();
CookieManager.getInstance().setCookie("http://alpha.mydomainname.com", cookieString);
}
System.out.println(CookieManager.getInstance().hasCookies()); //Prints false in 2.3, true in 4.0.3
CookieSyncManager.getInstance().sync();
System.out.println(CookieManager.getInstance().hasCookies()); //Also prints false in 2.3 and true in 4.0.3
}
I'm testing the same code in two different devices and the funny thing is, the cookies are set (and also transferred between launches of the application) correctly in 4.0.3 but not in 2.3.3. When I say they are not set, I mean that hasCookies() returns false and also getCookie() returns null when I provide the URL.
I've tried every possible combination for the Cookie URL when calling setCookie: "http://alpha.mydomainname.com", "http://www.mydomainname.com", "http://mydomainname.com", "mydomainname.com", "alpha.mydomainname.com", ".mydomainname.com", "www.mydomainname.com", none of them works. Please help.

I recently had a similar problem, and the following solution worked for me. I create/get instances of CookieSyncManager and CookieManager at the beginning, and use them throughout the code, instead of creating new instances again. I also had to experiment with setting the cookie on the correct domain - I had to set it to the domain that appears in one of the redirects.
final CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context);
final CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
//Save the two cookies: auth token and session info
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
String cookieString = cookie.getName() + "=" + cookie.getValue() + "; Domain=" + cookie.getDomain();
cookieManager.setCookie("http://mydomainname.com", cookieString);
}
cookieSyncManager.sync();
}

I've faced the same problem. There is not so clear how to use the setCookie method. You should use it with some loop if you have a few items with cookies (like in my case):
val cookies = "key1=someValue1;key2=someValue2;key3=someValue3"
val cookiesList = cookies.split(";")
cookiesList.forEach { item ->
CookieManager.getInstance().setCookie("http://someHost.com", item)
}
So you can't use it like:
CookieManager.getInstance().setCookie("http://someHost.com", "key1=someValue1;key2=someValue2;key3=someValue3")

I also ran into some strange behaviours with CookieManager, eventually I ended up with a solution that is a workaround - but it works.
Instead of using CookieManager I just used the http cookie headers, so for example using HttpUrlConnection it can look like that:
//Save the two cookies: auth token and session info
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
String cookieString = cookie.getName() + "=" + cookie.getValue();
myHttpURLConnection.setRequestProperty("Cookie", cookieString);
}
Of course, if you need to persist these cookies you will have to do it wourself and save their value somewhere for later use...

The cookie can't include the semicolon, because the semicolon means separator in cookies.

Related

Cookie doesn't work properly in webview in android

Needed datas is not written to cookies immediately in WebView. But when I wait for 10-15 seconds everything is ok.
To explain the situation, this example would be good as for me:
I open the app and login. After login, I close the app immediately. Then after I open the app again, it shows me to logout. But if I open the app after 1 minute, it shows again as logged in. For me cookies are written lately. But I cannot find solution. Please help me if you know.
I used CookieManager class but it doesn't help either.
CookieManager.getInstance().setAcceptCookie(true);
I had similar issue and I added the below code and worked.
String myURL = "https://www.yourWebPage.com";
android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.acceptCookie();
cookieManager.setAcceptFileSchemeCookies(true);
cookieManager.getInstance().setAcceptCookie(true);
cookieManager.getCookie(myURL);
Hope it helps.
I had the similar problem, what I did is to get the cookies when login and set cookies for the load url you set on webView
#Nullable
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String resourceUrl = request.getUrl().toString();
Log.e(MainActivity.TAG, "the request url :" + resourceUrl);
CookieManager cookieManager1 = CookieManager.getInstance();
// get the resourceUrl that has session
if (resourceUrl.equals("the url has session")){
String Cookies = cookieManager1.getCookie(resourceUrl);
if (Cookies != null && Cookies.contains("sessionid")){
String[] cookiesList = Cookies.split(";");
cookieManager1.removeSessionCookies(null);
for (String c : cookiesList) {
cookieManager1.setCookie("the load url", c);
}
}
}
return super.shouldInterceptRequest(view, request);
}

How to set cookie in Android WebView

I am facing issues with setting cookie in Android WebView. I am using the following code:
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(url, cookie);
CookieSyncManager.getInstance().sync();
But it's not working. I also tried by adding headers using WebView's loadUrl(String url, Map<String, String> additionalHttpHeaders) method; but still it's not working. Is there a way to do this?
Please try this
Cookie sessionCookie = LoginWebView.cookie;
CookieSyncManager.createInstance(webview.this);
CookieManager cookieManager = CookieManager.getInstance();
if (sessionCookie != null) {
cookieManager.removeSessionCookie();
String cookieString = sessionCookie.getName();
Log.v(TAG, "sync cookies: " + cookieString);
cookieManager.setCookie(domain, cookieString);
CookieSyncManager.getInstance().sync();
}
The CookieSyncManager is deprecated now, instead use cookieManager.flush(); to update.
See this: http://developer.android.com/intl/es/reference/android/webkit/CookieSyncManager.html

Cookie setcookie is not reflecting

I am trying to set cookie using API CookieManager in webview for sencha page.
CookieSyncManager.createInstance(this);
cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(domain, cookie);
But session is not reflecting when page is loading in webview.
This is a working bit of code.
private void setCookie(DefaultHttpClient httpClient, String url) {
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
if (cookies != null) {
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
for (int i = 0; i < cookies.size(); i++) {
Cookie cookie = cookies.get(i);
String cookieString = cookie.getName() + "=" + cookie.getValue();
cookieManager.setCookie(url, cookieString);
}
CookieSyncManager.getInstance().sync();
}
}
Here the httpclient is the DefaultHttpClient object you used in the HttpGet/HttpPost request. Also one thing to make sure is the cookie name and value, it should be given
String cookieString = cookie.getName() + "=" + cookie.getValue();
setCookie will the set the cookie for the given URL.

CookieManager.getCookie() returns null

I have some http requests. One of them retrieves and parse cookie from it's response. I save this cookie via CookieSyncManager and CookieManager with following code:
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeSessionCookie();
cookieManager.removeAllCookie();
String cookieString = cookie.getName() + "=" + cookie.getValue();
Log.e(getClass().toString(), cookieString);
cookieManager.setCookie(START_PAYMENT_URL, cookieString);
CookieSyncManager.getInstance().sync();
Log.e(getClass().toString(), "Get cookie: " + cookieManager.getCookie(START_PAYMENT_URL));
Both Log.e calls write same cookie. So everything looks ok.
I have different activity which contains WebView. I need to call postUrl(String url) method with some POST params and with authorization cookie. I thought that cookie is in CookieManager and everything should work great. Well. It is. But on 4.x devices only.
On 2.x devices WebView makes postUrl without cookie.
Here is activity code which contains WebView:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.payment_webview);
final String billId = getIntent().getStringExtra(INTENT_BILL_ID);
final WebView webView = (WebView) findViewById(R.id.payment_webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e(getClass().toString(), url);
view.loadUrl(url);
return false;
}
});
String postData = "id_bill=" + billId;
Log.d(TAG, "Requesting payment URL " + START_PAYMENT_URL + " with post data: " + postData);
Log.d(TAG, CookieManager.getInstance().getCookie(START_PAYMENT_URL));
webView.postUrl(START_PAYMENT_URL, EncodingUtils.getBytes(postData, "BASE64"));
}
CookieManager.getInstance().getCookie(START_PAYMENT_URL) returns null on 2.x devices and cookie value on 4.x devices.
How to fix this problem?
Seems like I figured out what's the problem.
I just removed
cookieManager.removeSessionCookie();
cookieManager.removeAllCookie();
while I'm saving cookies.
I suppose that problem is that all methods of CookieManager work asynchronously. Probably remove cookies methods invoked after cookieManager.setCookie(START_PAYMENT_URL, cookieString); even if they called before it. So CookieManager saves cookies and after that remove cookies methods invoked.

cookieManager outside of activity

I'm trying to do some post request to a webpage containing json data... but there are cookies involved.
cookies are working fine but aren't persistent....
I'm doing the request from a separate class(object). I pass the activity context to that class but I'm still not able to store the cookies.
I tried using cookiessyncmanager to sync the cookies but this requires a cookiemanger. and that's where I'm stuck because the cookiemanager doesn't allow me to create a context like the cookiesyncmanager does...
here's my code:
for(Cookie cookie : cookieStore.getCookies()){
String cookieString = cookie.getName() + "="
+ cookie.getValue() + "; domain=" + cookie.getDomain();
CookieManager.getInstance().setCookie(cookie.getDomain(),
cookieString);
}
CookieSyncManager.createInstance(baseContext).sync();
as you can see CookieManager allow allows the getInstance() method, but this just results in "failure" as the object obviously doesn't have a context....

Categories

Resources