Your flex app is deployed on a web site, and you'd like to access that sites "state" information without AJAX or a javascripting to the "wrapper". Example: Your website maintains "session" vars, one might be authenticated = true. If your flex app uses remoting and webservice calls it can't retrieve session vars as a ws call is a stateless call by nature.
The solution is to make a call via HTTPSERVICE to a URL of scripted page (CF, asp, .net , xml, whatever) that returns the state information (as xml) .
Scenero:
Elsewhere on the site:
When a user logs into the website, a session variable is created. A value of Authenticated=true is set. There are many way to build web page login's so I'll spare you with the details here.
The core idea is that your existing site creates and maintains some sort of session variables, and you want to access the values from flex in a dirt simple manner.
Most alternate methods of getting cookie or session vars require communication with the "wrapper" or page that the flex ap is deployed on.
One might make the page a .cfm and then output the session vars as "flashvars" in the page markup. Or one might use the Flex-AJAX bridge. Decent techniques but might be overkill if you want to deploy a flex app within and existing site without too much effort
This concept is SLY (Simple, Lean,Yes!) - Flex calls a page, page returns value to flex.
You might be thinking gee what's the woop?
Webservice and Remoting calls simply can't retrieve the "browser" state information directly.
Using a HTTPService call with "GET" as the method means the page called will be "processed" and then sent back to flex. Since flex is running in the browser, which mean if there was a browser session initated before you visit the flex page then you can retrieve the session info.
***************
Authenticate.cfm (coldfusion here, could be you are familiar with)
When this file is called up it returns true of false, based on whether the session exits, and if so, is the session authenticated.
**********************
<cfprocessingdirective pageencoding = "utf-8" suppressWhiteSpace = "Yes">
<cffunction name="ckAUTH" access="remote" returntype="boolean">
<cfscript>
// check for session
if (isDefined('SESSION')){
// session true
if (session.authenticated eq true){
// authenticated is true...
cond = true;
}ELSE{
// not authenticated
cond = false;
}
}ELSE{
// no session defined.
cond = false;
}
</cfscript>
<cfreturn cond>
</cffunction><cfoutput>
<cfxml variable="userXML">
<authenticated>#ckAUTH()#</authenticated>
</cfxml></cfoutput>
<cfoutput>#userXML#</cfoutput></cfprocessingdirective>
Simple enough, the page returns an xml packet...
<authenticated>true</authenticated>
or
<authenticated>false</authenticated>
Now on the flex side.
This example calls authenticate.cfm via httpservice, and displays a message based on whether the value is true or false. Since it's a HTTP GET call, the coldfusion script will retrun the session var to the page as it's processed and then the script sends back an xml packet In Flex, we cast AuthService.lastResult.authenticated as Boolean and use that value for further texting.
**** demo.mxml *****************
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
creationComplete="{AuthService.send()}"
>
<mx:Script>
<![CDATA[
import mx.utils.ObjectUtil;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
public function userFaultHandler(e:FaultEvent):void{
// do something if http returns an error
Alert.show(e.message.toString());
}
public function authServiceHandler(e:ResultEvent):void{
// do something based authenticate value
var cond:Boolean = AuthService.lastResult.authenticated as Boolean;
if(cond){
statusMsg.text = "Welcome to the member's area";
}else{
statusMsg.text = "Member's only area: Please Log in ";
}
}
]]>
</mx:Script>
<mx:HTTPService
id="AuthService"
url="http://www.flashmxwebbuilder.com/sites/ppwdemo/demo005/apps/findwines/authenticate.cfm"
method="GET"
fault="userFaultHandler(event)"
result="authServiceHandler(event)"
useProxy="false"/>
<mx:TextArea text="{AuthService.lastResult.authenticate}" id="statusMsg" width="424" height="200"/>
</mx:Application>