4 Replies Latest reply on Nov 10, 2011 11:13 PM by Harish R

    How do Cookies work in Flex?

    camostk

      Let's start with the goal. I need cookies to work as they do in any 'normal' browser.

       

      I have a Flex application that will be calling a webservice(REST service at url http://www.abc.xyz/setcookie) and the service returns a response and a cookie.  The service actually only records any cookies that it gets and returns another cookie along with a message of what it got in the cookie and what it set in the cookie. If I call this service in a browser (Mozilla) then the first time through there is no cookie but one is set and returned.  The 2nd time calling with Mozilla I get the message that there is 1 cookie in the request and one is set and returned. All is well.

       

      I code a flex application to simply call this service and try to decode the response. But am having problems.

       

      1. Can't find the cookie in the ResultEvent. Look in the headers that are available (using Debug) but they don't seem to be there. Are they in the container? (Browser)?

      2. When I make an HTTP Request using an HTTPService

       

          <mx:HTTPService id="restServiceSetCookie"
              url="http://localhost:8080/SimpleRESTapp/setcookie"
              result="resultHandler(event)"
              fault="faultHandler(event)"
              />      

      ... and then call it multiple times, the server always records that 0 cookies were sent with this request. I would have expected that it would be 0 the first time and then it would be 1 the next time as the cookie that was set by the server the first time was returned. The result handler simple prints out the text message from the server saying how many cookies it got with the request and the cookie data.

       

      Am I missing something fundamental in how Flex or the container (Browser) works with regard to cookies? Do you really have to reimplement cookie handling with every Flex application? I don't want to use Local Shared Objects as I would have to change the services that are being used presently. Any help or direction to look would be appreciated.

       

      Thanks

       

      Cam

        • 1. Re: How do Cookies work in Flex?
          Richard_Abbott Level 3

          Cam,

          there is no native support for cookies in AS3, but have a look at

          http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&extid=1724523

          or search for CookieUtil in the Adobe Flex libraries. This is a small class that you can add to projects and then get/set at will. I have found it very easy to use.

          Richard

          • 2. Re: How do Cookies work in Flex?
            camostk Level 1

            Thanks Richard. I was aware of that library but was really wishing that Flex would handle cookies in some built-in/reasonable fashion since they are so ubiquitous.  Just for giggles, what actually happens to the cookies? I.e. it looks to me like the HTTPService class must make it's call to the remote service without including cookies that would normally be passed by the browser and the same in response. ie. the browser or someone must strip the cookies from the response before passing it to the Flash VM. A complete HTTP response would include the cookies would it not?

            I ran the response from the server in debug mode and hunted through the ResultEvent to the my call to this remote service that sets a cookie in the response and there was no where in the ResultEvent  that had the cookie or anything resembling a cookie. So something is actively stripping them. It may just be the browser/container but it seems very strange to me that the response would not be complete when it is returned to the Flex VM.

            Does anyone have any further information on this?

             

            Thanks

            Cam

            • 3. Re: How do Cookies work in Flex?
              leybniz Level 4

              flash.net.URLRequest

               

              AIR-only manageCookies : Boolean

              Specifies whether the HTTP protocol stack should manage cookies for this request.
              Works for AIR only.
              • 4. Re: How do Cookies work in Flex?
                Harish R

                Hi Cam,

                 

                Im working on a air app with the smae requirement as yours, do you got any luck with your project. If so please help me reading the cookies.