Accessing browser cookies from Flex

◇◆丶佛笑我妖孽 提交于 2019-12-28 04:23:13

问题


I'm building a Flex widget for a private vBulletin site, and the Flex widget needs to access an XML file on the vBulletin server in order to display data.

For security reasons, the XML URL will need to have the value in the bbsessionhash cookie passed along in the URL request from Flex. The Flex widget will be embedded in the private area that the user has logged into, so the Flex request will be coming from the same website the cookie is from.

Is there any way to access the cookies directly within Flex? I would prefer not to use ExternalInterface to grab the cookie data from JavaScript, as it could get a little messy (the templates are developed by a completely different dev team).


回答1:


I have never tried this, but this library might just do the trick.




回答2:


As per the flash or flex cookies are concern developer can use shared object which is one kind of cookie used for flex application.

The sample code snippet is as followes

import flash.net.SharedObject;

// get/create the shared object with a unique name.
// If the shared object exists this grab it, if not
// then it will create a new one
var so: SharedObject = SharedObject.getLocal("UniqueName");

// the shared object has a propery named data, it's
// an object on which you can create, read, or modify
// properties (you can't set the data property itself!)
// you can check to see if it already has something set
// using hasOwnProperty, so we'll check if it has a var
// use it if it does, or set it to a default if it doesn't
if (so.data.hasOwnProperty("theProp"))
{
    trace("already has data! It reads: " + so.data.theProp);
}
else
{
    so.data.theProp = "default value";
    so.flush(); // flush saves the data
    trace("It didn't have a value, so we set it.");
}



回答3:


Accessing Flex SharedObject is NOT the same as accessing the Browser cookies, to access the browser cookies, you may use the ExternalInterface class, please check the following reference to see samples:

http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_4.html

A reference of how to use and control cookies using JavaScript can be found here:

http://www.quirksmode.org/js/cookies.html

I would use the following Flex code:

var myCookie:String = ExternalInterface.call("getCookie('cookieName')");

And in the HTML I would add the following Javascript:

function getCookie(c_name) {
  var i,x,y,ARRcookies=document.cookie.split(";");
  for (i=0;i<ARRcookies.length;i++) {
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
    x=x.replace(/^\s+|\s+$/g,"");
    if (x==c_name) return unescape(y);
  }
}

If you require more help you could also check the Flex documentation.



来源:https://stackoverflow.com/questions/523633/accessing-browser-cookies-from-flex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!