- Home /
Present cookie data in WWWForm?
Hi again! I've been wondering, and it's not that important, but is it possible to actually send cookie data into a WWWForm? For example, I could somehow set "thiscookie" to "isyummy", send that to the WWWForm, and have any PHP cookie requests for "thiscookie" return "isyummy"?
Answer by qJake · Aug 02, 2010 at 04:48 AM
No, but you can send URL parameters via GET/POST that tell PHP to set cookies for you, and then you can retrieve those cookie values from PHP. That, or you could write a JavaScript (web-javascript, not UnityScript, they're different) to get and set cookie values for you, and then you could call those from within the game using the WWW class. Unity can't directly get or set cookie data, though, no.
Thanks for the info! I can call JS into WWW instances - As in run JS on the webpage that I'm loading? Or does this only work within the webpage that a web player is in?
You can only run JavaScript that the Unity webplayer is in, which is the currently loaded page... this is true for all webpages.
Answer by Bunny83 · Jan 23, 2011 at 12:14 AM
Actually you can. Just setup your custom headers like in this example: WWWForm Headers You need to add the "Cookie" header. (HTTP Headers) That should do the job (not tested). There could be a difference between Web and Standalone.
the annoying problem is, you can't do this
WWWForm myform = new WWWForm();
myform.AddBinaryData("abc", bytes );
Hashtable jsonHeaders = new Hashtable();
jsonHeaders.Add( "Cookie", theCookie );
WWW w = new WWW(fullURL, myform, jsonHeaders);
yield return w;
you can't mix a WWWForm (which allows AddBinary and thence multipart), with the form of having headers on the end as the third argument.
So you can't (my final sentence was cutoff here, so sounded weird!)
Umm, well, you can, but you should use myform.headers to initialize your headers variable. This property simply returns the corect content type for the given form:
public Hashtable headers
{
get
{
Hashtable hashtable = new Hashtable();
if (this.containsFiles)
{
hashtable["Content-Type"] = "multipart/form-data; boundary=\"" + Encoding.UTF8.GetString(this.boundary) + "\"";
}
else
{
hashtable["Content-Type"] = "application/x-www-form-urlencoded";
}
return hashtable;
}
}
$$anonymous$$eep in $$anonymous$$d that WWWForm is just a helper class which should simplify the creation of a valid HTTP request. You can always setup the request yourself, but you should know what you're doing ;).
Ah - you've just made me realise of course obviously one can "change" items in the headers.
That's awesome thanks....it just didn't occur to me.
Another thing I realised which may help future readers
Like I mentioned "you can't mix a WWWForm (which allows AddBinary and thence multipart), with the form of having headers on the end as the third argument" because they don't offer that overload.
But of course there's the handy .data property on WWWForm
so that you can use yourForm.data as the middle argument to new WWW().
I believe there are three idioms possible:
string fullURL = "http://blahblah.com/image/add";
string cookieString = "sid=" + uid;
//method 1
Hashtable jsonHeaders = new Hashtable();
jsonHeaders.Add(
"Content-Disposition",
"form-data; name=\"xx\"; filename=\"newim.png\"" );
jsonHeaders.Add( "Content-Type", "image/png" );
jsonHeaders.Add( "Cookie", cookieString );
WWW w = new WWW( fullURL, bytes, jsonHeaders );
yield return w;
//method 2
WWWForm myform = new WWWForm();
myform.AddField("cookie", cookieString );
myform.AddBinaryData("fn", bytes );
WWW w = new WWW(fullURL, myform);
yield return w;
//method 3
var xform = new WWWForm();
xform.AddBinaryData("fn", bytes );
var headers = xform.headers;
var rawData = xform.data;
headers["Cookie"] = cookieString;
WWW w = new WWW( fullURL, rawData, headers );
yield return w;
Your answer
