- Home /
WWW responseHeaders not giving me cookie in web-player, but does in editor
I know this feature is undocumented except in the release notes, but it also appears to be buggy.
My PHP uses setcoookie() to set a cookie, of course, and when I'm running the editor, I can get it no problem. However, in web player builds, this header is not there, as if it didn't exist. I see other headers there, but not the SET-COOKIE one.
Try
for (var r in www.responseHeaders)
Debug.Log(r);
What gives?
Answer by Bunny83 · May 10, 2011 at 02:01 AM
The problem could be that the cookie is still set. The Webplayer does the www communication through the browser. A webserver will set a cookie only if needed. Since the cookie is transmitted with each www call there's no need for the server to set the cookie.
You can use Application.ExternalEval to get the cookie information from document.cookie
. Furthermore the responseHeaders have a bug. They made it a Hashtable which seems quite handy in the first place but some HTTP header fields (like Set-Cookie) can be added multiple times. The Hashtable will only keep one of them... Some servers merge all Set-Cookies into one field but not all.
If you're not sure if the field is included or not i recommend WireShark ;)
edit
Well, just put this script on a GameObject and name the object "MyObject" (or what ever)
var cookieStr : String; var isLoaded = false; function UpdateCookieString() { isLoaded = false; Application.ExternalEval( "var unity = unityObject.getObjectById(\"UnityContent\");"+ "unity.SendMessage(\"MyObject\", \"OnGetCookie\", document.cookie);" ); }
function OnGetCookie(cookie : String) { cookieStr = cookie; isLoaded = true; }
Just make sure that the SendMessage function can find your GameObject. See the browser communication docs for more information.
ps. you can insert the object name directly into the eval string:
"unity.SendMessage(\"" + gameObject.name + "\", \"OnGetCookie\", document.cookie);"
Yeah I saw your earlier post about this (thanks for that too!) but I doubt that it's getting intercepted by the browser. I suppose it's possible, but I'm making a www object request from within the player, after the scene is loaded. I'll try a header sniffer. Worst case I'll have it not use cookies.
I used LiveHeaders in Firefox, and yup, my cookie is there. So why wouldn't it get passed down into the www response? I just have the one cookie. I hope they fix this next release. Also seems to depend on the browser. Firefox and IE ok, Chrome not.
Bunny83, Q for you: How would you use ExternalEval to get the cookie? It doesn't return a value. Would you have it call a function which echos back a Send$$anonymous$$essage to set it on an internal function?
I've added an example that should work ;). not tested ...
Your answer
Follow this Question
Related Questions
Workaround for SET-COOKIE bug in www.responseHeaders? 2 Answers
How do I properly send binary data (byte[]) to a MySQL database? 4 Answers
Having Trouble with Javascript GET and URL 1 Answer
Failed downloading http with WWW on Internet Explorer 0 Answers
Bytes array always empty in WWW object (on WebPlayer) 0 Answers