• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Flynn · Aug 02, 2010 at 03:27 AM · wwwformcookies

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"?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Flynn · Aug 02, 2010 at 05:43 PM 0
Share

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?

avatar image qJake · Aug 02, 2010 at 06:56 PM 0
Share

You can only run JavaScript that the Unity webplayer is in, which is the currently loaded page... this is true for all webpages.

avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jun 20, 2013 at 04:28 PM 0
Share

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!)

avatar image Bunny83 · Jun 21, 2013 at 12:54 AM 0
Share

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 mind 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 ;).

avatar image Fattie · Jun 21, 2013 at 05:07 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

PHPSESSID with Unity 2 Answers

Problem with online high score 4 Answers

Web service in a button event handler 2 Answers

How do I use WWWform and JSON to login to reddit? 1 Answer

Unity Facebook 400 Bad Request 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges