Hi, I’m having an issue with Unity.Webrequest, my code works fine in the editor, but it doesn’t when I build to Android.
My code logs in a user using a name and password and then gets a value from the server that it has to remember in order to identify itself the next times it serves or requests data. Getting this value works fine, but sending it back is failing on Android, while it works fine in the editor.
I don’t see anything wrong with this code.
So though debug logging you see that the cookie is correct on Android?
Have tried to setup some proxy to monitor network traffic?
But this doesn’t work. The debug shows that the header is null before I add my own cookie header, and it logs as it should be after I set it.
I think this means that Android appends it’s own header after Unity did what it is supposed to.
For now I made it work by simply commenting out the line where I set the cookie, meaning that I now have code that depends on Android taking care of this automatically.
I am still a bit worried though that this won’t happen the same way on all current and future versions of Android, so if there is a way that assures my header is set properly and only once, I am still interested in a better solution.
On Android there is a CookieManager in UnityWebRequest so it is expected for cookies to be sent automatically. There is no such thing in Editor for now. We are working on this to make it more consistent across platforms, but that’s quite the future now.
hi @Aurimas-Cernius , with our api, the header, if not setup correctly we will get error response in json. The error is not about isNetworkError or isHttpError. Actually I have checked using postman app, and it is working well. Then we try in unity, it still get error. I don’t know why. I assume when i call setrequestheader need another way. if you have advice to solve myproblem, it would be great, thanks
oh yaaa, i forgot something, when i call setrequestheader like mycode before and when i dont call setrequestheader in my code, both of them show the same error response in json.
I don’t know if it late to continue this discussion or not but i ran to same problem 2 day ago just in android devices , I get unauthorized access , my code works fine in IOS and editor , I install an emulator and monitor the traffic with wireshark and figure out that just first time you want to call a web service an automated cookie is set by device so you have 2 cookies in your request . but as soon as you kill your app or quit it it wont set cookie automatically and everything works fine afterward , now the hard part is how to recognize if user close the app one or not ,to set a flag
is there any way to know what headers is in a request ?
Thank for the replay
First of all I am using unity2018.2
and yes I am setting cookie by my self and i dont think in this version they are set automatically except the first call from android deceives
my code work fine in IOS and Editor but fist call from android devices will send 2 cookies and get access denied from server but if i kill the app and run it again everything works fine
2018.2 does have a cookie handler, so cookies do persist within same session for sure. We do set CookieManager object to handle cookies, if none is set.
We only do set it if it’s not set yet and only once, so if you set it yourself to something that does nothing, you should be fine.
Our code for cookies is as simple as:
if (CookieHandler.getDefault() == null)
CookieHandler.setDefault(new CookieManager());
That is Java code. If you want to call it from your script, you have to use AndroidJavaClass and it’s companions or write pure Java code and invoke that.
Sure you can. But you have to check first if you need to - in a single app instance cookies are managed automatically. To make them work across the session, you may need to implement it yourself. Note, that on some platforms they do work across the sessions too, but that’s platform dependent. Not so on Android.