How to set programmatically cookies using Crosswalk XWalkView? - android

I need to make a webview application that POST a username and password and then display the webpage logged-in. I must use cookies but i chouldn't find how to do it, tried the traditional ways but nothing.
Has anyone had any experience with xWalkView and how to get/put cookies ?
(I am using xWalkView since the traditional webview on android 4.2.2 can't load HTML5 webpages)

I had the same problem but I did not find the exact answer. Instead, I found a kind of a work around.
As I am using a servlet container, I append a ";jsessionid=" to every request I make to the server.
The other thing than can actually work for you regardless of what backend you use is to make a request to your web server so that it returns a response with a cookie.
For example if you want to add a name=test you could make some xhr request to some url and the response must have the cookie name=test in it. This technique also work for me for session tracking in my java app.
Good luck

Related

How to simulate browser login in Android using API

I want to make an android app which will login to my web application using rest API. In browsers we have a concept of cookie which servers use to identify/maintain session with the users.
In Android how would we accomplish it ? I heard that there is a concept of token which is sent by server in response(first time when credentials are validated) and Android app have to send it to server every time it tries to access a resource(protected). So, what is the better way of doing it ?
Do we need to validate the token again and again when the client requests for a resource ?
Honestly, I can't think of a better way of doing this. Token based authentication seems to be pretty standard when dealing with RESTful APIs. Is there any reason you can't do that?
If you don't want to change the server code, then this could be simulated by adding a cookie header to every request you send. But this is basically the same thing that you mentioned above, just not as clean.
And the browser is already sending a token to be validated again and again. Every request has a cookie header that gets validated through your web application on every request, so this isn't a big deal at all.
And, you don't need anything Android specific to accomplish this. In whatever http library you're using I'm sure there is a method you can called or something you can override in order to set custom headers. Use that to set either your cookie header or token header on every request that you need to make.

Setting cookies when php is accessed by android app

(I'm asking this partly for learning purposes, I realize what I'm trying to do might be entirely wrong!)
I have a php file on my website that handles log in and sets a cookie for the user if log in is successful. if setcookie() fails, I error out instead of displaying the rest of the page.
When I try to access this page using my android app (which uses HttpURLConnection with POST), the setcookie() fails. I'm guessing this is because the client isn't a browser and can't handle cookies.
so first of all, is there away for my app to be able to receive cookies from the server and store them? if not, how do you handle maintaining a login session with the user so you dont have to send a username and password, every time you want to access data from the server?
THanks
A couple of notes before the workaround:
The function is called setcookie() not set_cookie()
Android browsers do support cookies afaik, so you probably should look into this further. Perhaps the format of your setcookie call is not valid?
If you can't use cookies, then the workaround is to simulate your own session mechanism by passing your cookie value as a url parameter on every request.

Android authentication and Web

I set up Contacts and Synchronization in the application I am developing. Authentication works in directly to the web site using DefaultHttpClient. That means I need to store cookies and every time user does something send it to server using that cookie. What is the better way to store the cookies? It seems CookieSyncManager only works with WebView. Or can I use it with DefaultHttpClient?
In the webview, there is a setting. I think this may help you:
WebSettings.setSavePassword(boolean save);

cookies getting cleared on closing a webapp in android

I am working on a web application for android phones, which is basically few js and html files packaged using Phonegap for android. I am making http requests to the server, getting some cookies (whose life is 10 yrs). These cookies are set by the response header. Now this works fine for this session, the set cookies are sent with each request. But if a quit the app and restart it, the cookies vanish, and are not sent with the request.
The life of the cookies is 10 yrs. Shouldnt they persist? Please tell me where i am getting it wrong?
EDIT-- I tried saving the cookie in an sqlite db, and then setting it properly in document.cookie before making the ajax call. Still its not being sent. Any ideas...?
Cookies wont persist after the app is closed. Also you cannot directly set the cookies in an xhr object using javascript, according to w3c specifications, so thats why i wasnt able to do that. the solution would be to re-perform the actions which set the cookies in the cookie jar in the first place.

How to send html form using to https resource in Android?

I only want to send an html form by post method to a https resource from an Android activity.
I have (only for development and testing) an Lighttpd server with it own certificate to make handshaking, so there MUST happen authentication at least from server (client authentication is optional but desirable).
I have seen, lots of forums with different ways to make it, but I am a little confused, I do not know which could be the correct way to make it.
Please show me a chunk of code.
Thank you very very much.
Try out this tutorial, it shows GET, POST and Multipart POST request on the android platform
http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
I wrote some code to submit an HTML form to a server over https, which can be found in this answer. The version in the answer uses an HttpsUrlConnection, and the version in the question uses HttpClient. I could never quite get the right result from the server with the HttpClient version, but either approach should work in theory.

Categories

Resources