[转]通过Flex读写浏览器中的cookie的方法整理
原文链接:http://www.bennyxu.com/?p=144
一直以来都想做个网站管理的编辑器,但是我预估到的最大问题就是用flex来访问和读取网站,其中就用到了登陆校验的cookie,但是找了好多的网站,都没有找到相关的,信心,只是在adobe的官方的api中介绍了新增的htmlLoader 的相关属性方法,无济于事,自己去研究,调了又调,试了又试的,终于向客户端写cookie成功了,跟大家分享一下:
其实有两种方法可行:
方法一:是再模板html中添加一段js写cookie的代码,再flex端用ExternalInterface.call来调用,感觉麻烦,没用这种方法:
方法二:直接在flex端向客户端写cookie
关键代码是:
<code>
//向客户端写入cookie
private function setMyCookie(name:String,value:String) :void{
ExternalInterface.call("function (name,value) " +
"{ " +
" var Days = 30; "+
" var exp = new Date();"+
" exp.setTime(exp.getTime() + Days*24*60*60*1000); "+
" document.cookie = name+ '='+ escape (value) + ';expires=' + exp.toGMTString();" +
" }",name,value);
}
</code>
完整的用户登录代码,当用户登录成功时,调用向客户端写cookie的代码
<code>
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" xmlns:code="http://code.google.com/p/flexlib/">
<s:states>
<s:State name="maxState"/>
<s:State name="minState"/>
</s:states>
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import net.shopin.service.*;
import net.shopin.vo.SysUserVO;
import net.util.ShareScript;
private var sysUser:SysUserVO = null;
//公共变量
private var shareFunc:ShareScript = new ShareScript();
//监听用户的键盘回车事件
private function keyHandler(event: KeyboardEvent): void {
if (event.keyCode == 13) {
login();
}
}
//退出系统
private function logOut():void
{
logs.visible = true;
shareFunc.clearShare();
resetHandle();
}
//用户登录
private function login():void {
if (username.text=="" || password.text=="") {
Alert.show("用户名和密码不能为空");
}else{
var service:ReportService = new ReportService(resultLogin);
service.login(username.text);
}
}
//登录回调信息
private function resultLogin(data:Object):void {
sysUser = data as SysUserVO;
if (sysUser.loginName != null && sysUser.userPassword == password.text) {
logs.visible = false;
if (rec.selected) {
shareFunc.writeShare(username.text, password.text);
}
setMyCookie(sysUser.loginName,sysUser.userPassword);
} else {
password.text = "";
Alert.show("用户名或密码有误,请重新输入!");
}
}
//重置
private function resetHandle():void
{
username.text = "";
password.text = "";
}
//向客户端写入cookie信息
private function setMyCookie(name:String,value:String) :void{
ExternalInterface.call("function (name,value) " +
"{ " +
" var Days = 30; "+
" var exp = new Date();"+
" exp.setTime(exp.getTime() + Days*24*60*60*1000); "+
" document.cookie = name+ '='+ escape (value) + ';expires=' + exp.toGMTString();" +
" }",name,value);
}
]]>
</fx:Script>
<mx:Panel x="414.5" y="274" width="387" height="305" layout="absolute" title="管理员登录" fontFamily="Georgia"
fontSize="14" id="logs" visible="true" width.maxState="353" height.maxState="268" x.maxState="411.5" y.maxState="274" title.maxState="管理员登录">
<mx:Label x="41.5" y="33" text="用户名" fontWeight="bold"/>
<mx:Label x="51.5" y="81" text="密码" fontWeight="bold"/>
<mx:TextInput x="94.5" y="33" id="username" width="174.5" />
<mx:TextInput x="94.5" y="79" id="password" displayAsPassword="true" width="174.5" />
<mx:Button x="96.5" y="211" label="登录" id="btnLogin" click="login();" x.maxState="97.5" y.maxState="175"/>
<mx:Button x="198.5" y="211" label="重置" id="btnReset" click="resetHandle()" x.maxState="199.5" y.maxState="175"/>
<mx:CheckBox id="rec" x="96.5" y="166" label="记住用户名密码" height="37" x.maxState="97.5" y.maxState="126"/>
</mx:Panel>
</s:Application>
</code>