//按下按钮,打开网页 btnOpen.addEventListener(MouseEvent.CLICK,function(){navigateToURL(new URLRequest("http://www.g.cn/search?hl=zh-CN&q=" + encodeURIComponent(txtId.text)),"_blank"); });//以Get方式发送数据(发送就完事,不会理会服务端是否响应) btnSend.addEventListener(MouseEvent.CLICK,function(){sendToURL(new URLRequest("/default.aspx?q=" + encodeURIComponent(txtId.text))); }); btnPost.addEventListener(MouseEvent.CLICK,fnPostData);//以Post方式发送数据(同样:发送就完事,不会理会服务端是否响应) function fnPostData(e:MouseEvent) {var _urlReq:URLRequest = new URLRequest();_urlReq.url = "/default.aspx";_urlReq.method = URLRequestMethod.POST;var _data:URLVariables = new URLVariables();_data.q = "菩提树下的杨过"; //即传递 q = 菩提树下的杨过,注:经测试,Flash会自动对传递的数据做encodeURIComponent处理,所以此外不能再加encodeURIComponent,否则就是二次编码了_urlReq.data = _data;sendToURL(_urlReq); }
服务端可以这样处理:
protected void Page_Load(object sender, EventArgs e)
{string q = Request["q"];if (!string.IsNullOrEmpty(q)) {string _file = Server.MapPath("~/log.txt");File.AppendAllText(_file,q + "\t" + Request.HttpMethod + "\t" + DateTime.Now + Environment.NewLine);}
}
如果发送了数据后,还要响应服务端的结果(比如取得服务端的返回值,再继续到Flash中处理),Flash中可这样写:var loader:URLLoader = new URLLoader(); configureListeners(loader); var request:URLRequest=new URLRequest("/FlashHander.ashx?q=" + encodeURIComponent("菩提树下的杨过")); try {loader.load(request); } catch (error:Error) {trace("Unable to load requested document."); }//定义各种情况的回调函数 function configureListeners(dispatcher:IEventDispatcher):void {dispatcher.addEventListener(Event.COMPLETE, completeHandler);dispatcher.addEventListener(Event.OPEN, openHandler);dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); }//加载完成时,将触发 function completeHandler(event:Event):void {var loader:URLLoader=URLLoader(event.target);trace("completeHandler: " + loader.data);lblReceive.text = loader.data; //本例中,服务端返回: msg=Hello World&Method=GET&q=菩提树下的杨过var vars:URLVariables=new URLVariables(loader.data);trace("The Method is " + vars.Method); //服务端返回的字符串中如果有 Method=xxx 这样的字符,则Flash中可以直接用vars.Method进行访问 }//刚开始请求时,将触发 function openHandler(event:Event):void {trace("openHandler: " + event); }//下载进度发生变化时,将触发(可利用这个做加载进度条) function progressHandler(event:ProgressEvent):void {trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal); }//因安全原因出现错误时,将触发 function securityErrorHandler(event:SecurityErrorEvent):void {trace("securityErrorHandler: " + event); }//http请求状态变化时,将触发 function httpStatusHandler(event:HTTPStatusEvent):void {trace("httpStatusHandler: " + event); }//io错误时,将触发 function ioErrorHandler(event:IOErrorEvent):void {trace("ioErrorHandler: " + event); }
服务端FlashHander.ashx可以这样处理:
注意:返回的字符串格式为 name1=value1&name2=value2&name3=value3... 如果name和value中本身包含"="与"&",请注意用其它字符替换掉
///
/// Summary description for FlashHander
///
public class FlashHander : IHttpHandler
{public void ProcessRequest(HttpContext context){context.Response.ContentType = "text/plain";context.Response.Write("msg=Hello World&Method=" + context.Request.HttpMethod + "&q=" + context.Request["q"]);}public bool IsReusable{get{return false;}}
}