前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flash/Flex学习笔记(4):如何打开网页及Get/Post数据

Flash/Flex学习笔记(4):如何打开网页及Get/Post数据

作者头像
菩提树下的杨过
发布2018-01-23 15:02:52
6900
发布2018-01-23 15:02:52
举报

flash终究只是客户端技术,所以很多时候还是需要与服务端技术(比如asp,asp.net,jsp,php之类)进行数据交互的,下面的代码演示了如何在flash中打开网页,以及用GET/POST二种方式向服务端发送数据

代码语言:javascript
复制
//按下按钮,打开网页
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);
}

服务端可以这样处理:

代码语言:javascript
复制
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中可这样写:

代码语言:javascript
复制
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中本身包含"="与"&",请注意用其它字符替换掉

代码语言:javascript
复制
/// 
/// 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;
  }
 }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010-03-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档