Android Question Setting cookies for an HttpJob doesn't work for seemingly no reason.

Yuri Cinesi

Active Member
Licensed User
Here is what I'm basically doing: I check if a text file containing the stored cookies exists, if it doesn't then I use webviewextra to run some javascript on the login page and log in. Following that I use cookiemanager to extract the cookies and save them to the aforementioned file.
If the file does exist then an HttpJob is defined and I use Job.GetRequest.SetHeader("Cookie",File.Readstring(File.DirInternal,"LoginCookies") to read the file and add the cookies to the httpjob. Of course that line is after the download.

Now here's the tricky part. The code that handles getting the cookies works perfectly fine. However in my main project the cookies don't work. When I tried creating a smaller project to reproduce the bug I was unable to, despite the code handling the cookies being identical.

I have no clue why this happens. I've tried clearing the header - it did nothing. I have also tried clearing the project.

I'm honestly at a loss on this issue. Every variable is, as far as I can tell, the same between the actual project and the test case. Same page, same login, same download method. Any help would be GREATLY appreciated.
 

Yuri Cinesi

Active Member
Licensed User
It's just the webpage as if I weren't logged in. And I tested it again, it does indeed work perfectly fine on the other app by just copying the cookies the main one gets and putting them in that one.
 
Upvote 0

Yuri Cinesi

Active Member
Licensed User
Thanks to @OliverA's help (Thank you very much by the way) I've been able to figure out what the issue is. It seems that, for whatever reason, if any page of the website is loaded before trying to log in it will not work. If however I log in without downloading a page from the website it works fine. The job is being correctly released any everything.

@Erel I've edited @OliverA's demonstration of the issue to include an easy way to toggle it, but I cannot post it here as it contains my actual log in details. I will PM it to you.
 
Upvote 0

Yuri Cinesi

Active Member
Licensed User
It is correct. I don't know if you've tested the project I've sent yet, but it works just fine if the page is not loaded first.
The cookies can be re-used without issues - I've even tested that manually using a chrome extension.
 
Upvote 0

Yuri Cinesi

Active Member
Licensed User
Have you seen the project? The page is merely loaded without cookies at the very start of the activity. Meanwhile the page is loaded with the cookies every time the button is pressed.
I load the page at the start of the project to test if the website is up or not.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Have you seen the project?
Yes.

The page is merely loaded without cookies at the very start of the activity.
The server can manage sessions in all kinds of ways. It is possible that if you send a request to a different page without the cookies (?) it starts a new session and makes the stored cookies invalid.

You can see it here:

 
Upvote 0

Yuri Cinesi

Active Member
Licensed User
The server can manage sessions in all kinds of ways. It is possible that if you send a request to a different page without the cookies (?) it starts a new session and makes the stored cookies invalid.

I actually have one more thing I don't quite understand.
The workflow of the example app is as follows:
App is started > Page is loaded without cookies (Possibly expiring the stored ones) > LoginUpdate runs to update the stored cookies > App attempts to download the page again using these new cookies.

What I don't understand is why the new download still fails despite the cookies being new and the page not having been loaded without them since they were obtained.

I know for a fact these new cookies have not expired yet as if I were to restart the app without the option to load the page without the cookies beforehand they will work.

Oh and if I were to start the app with the option to load the page without the cookies (theoretically expiring them) and then shut it down before the cookies are updated and restart it without that option enabled they still work. Should they not have expired due to the page without cookies having been loaded beforehand?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…