I am setting a cookie using cookiemanager setcookie API, when I do cookiemanager getcookie I am not getting the domain & expiry date below is my code.
String cookieString = cookie.getName() + "=" + cookie.getValue() + "; Domain=" + cookie.getDomain()+"; expires=" + cookie.getExpiryDate();
CookieManager.getInstance().setCookie("http://mydomainname.com", cookieString);
String cookie = CookieManager.getInstance().getCookie("http://mydomainname.com");
Any idea why cookiemanager getcookie doesn't return domain & expiry date. Is it a bug ?
Thanks,
Kumar.
Not a bug - just read the documentation. The Android API docs say that getCookie() returns a string "using the format of the 'Cookie' HTTP request header"
The format of the Cookie: request header is a list of cookie name/value pairs separated by semicolons - it does NOT contain domain or expiry information, since a cookie is sent only if the URL matches and it has not expired. The CookieManager.getCookie() method does the same - it returns all the cookies that match the URL supplied and which have not expired.
Related
I have a Xamarin Forms app where I share cookies between the Webview and HttpClient by grabbing them after a login. On iOS this works fine, on Android I have the following issue:
If the cookie is created as the result of a HTTPClinet Api call, then deleted (expired) using either a WebView or HttpClient the cookie is no longer in the cookie list. When using HttpClient the HttpClientHandler.CookieContainer has a count of 0.
If the cookie is created using a WebView and deleted using another WebView the cookie is no longer in the cookie list.
If the cookie is created using a WebView and deleted using a HTTPClient Api call the expiration does not happen and the cookie is still in the HttpClinet's HttpClinetHandler's CookieContainer, I can see that the count is not 0.
If I look at the HttpResponse I see the expired cookie in the header:
"MyTestCookie=; expires=Wed, 28-Feb-2018 21:25:08 GMT; path=/; HttpOnly"
If I look further into the CookieContainer the m_domainTable has 2 entries, one for my ip with no cookies, and one for my ip preceded with a "." that contains the cookie that should be expired/deleted but it is not expired and has the original value.
The server code that creates the cookie for both the Api call and MVC Page is:
var cookie = new HttpCookie("MyTestCookie");
cookie.HttpOnly = true;
cookie.Values["token"] = "309d530f956ac04";
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
and the code that Deletes / Expires it is:
if (Request.Cookies["MyTestCookie"] != null)
{
var cookie = new HttpCookie("MyTestCookie");
cookie.HttpOnly = true;
cookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(cookie);
}
Is this a bug, or am I missing something?
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.
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 have been creating an app which consist of a lo-gin page for a site. I am using HttpGet method and DefaultHttpClient for making connection. Everything works fine in with this. I will be receiving json response once the lo-gin is successful , in which i will be getting an URL to load with web-view. While loading the URL in web-view the cookies is not getting sync.I have been breaking my head for past one week. Can any one help me to solve this problem.Thanks in advance.
Finally after spending a 10 days i found a solution.
if (!Utils.cookies.isEmpty()){
CookieSyncManager.createInstance(WebViewActivity.this);
CookieManager cookieManager = CookieManager.getInstance();
for (Cookie cookie : Utils.cookies) {
Cookie sessionInfo = cookie;
String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();
cookieManager.setCookie(sessionInfo.getDomain(), cookieString);
CookieSyncManager.getInstance().sync();
}
}
this piece of code worked for me.
For newer APIs , i.e API 21(Lollipop) and above, CookieSyncManager has been deprecated and CookieSyncManager.getInstance().sync(); can be replaced by CookieManager.getInstance().flush(); to write cookies to a persistent storage.
I'm building a Cookie Base Login form for a Website.
But the Login doesn't work on any Tablet. The Cookies are missing on some Tablet's.
If I use an other Sim-Card it works. If i use Firefox to login it works to.
But the normal Browser loose the Cookies.
Any Idea what i can do?
You can try using CookieManager to retreive cookie at some step, and then set it later:
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().sync();
CookieManager cookieManager = CookieManager.getInstance();
Cookie cookie = cookieManager.getCookie(url);
// .. and later
String cookieString = cookie.getName() + "=" + cookie.getValue();
cookieManager.setCookie(cookie.getDomain(), cookieString);
CookieSyncManager.getInstance().sync();