ash终究只是客户端技术,所以很多时候还是需要与服务端技术(比如asp,asp.net,jsp,php之类)进行数据交互的,下面的代码演示了如何在flash中打开网页,以及用GET/POST二种方式向服务端发送数据
02 |
btnOpen.addEventListener(MouseEvent.CLICK, |
08 |
btnSend.addEventListener(MouseEvent.CLICK, |
10 |
sendToURL( new URLRequest( "/default.aspx?q=" + encodeURIComponent (txtId.text))); |
12 |
btnPost.addEventListener(MouseEvent.CLICK,fnPostData); |
15 |
function fnPostData(e:MouseEvent) { |
16 |
var _urlReq:URLRequest = new URLRequest(); |
17 |
_urlReq.url = "/default.aspx" ; |
18 |
_urlReq.method = URLRequestMethod.POST; |
19 |
var _data:URLVariables = new URLVariables(); |
1 |
protected void Page_Load( object sender, EventArgs e) |
3 |
string q = Request[ "q" ]; |
4 |
if (! string .IsNullOrEmpty(q)) { |
5 |
string _file = Server.MapPath( "~/log.txt" ); |
6 |
File.AppendAllText(_file,q + "\t" + Request.HttpMethod + "\t" + DateTime.Now + Environment.NewLine); |
如果发送了数据后,还要响应服务端的结果(比如取得服务端的返回值,再继续到Flash中处理),Flash中可这样写:
01 |
var loader:URLLoader = new URLLoader(); |
02 |
configureListeners(loader); |
03 |
var request:URLRequest= new URLRequest( "/FlashHander.ashx?q=" + encodeURIComponent ( "菩提树下的杨过" )); |
06 |
} catch (error:Error) { |
07 |
trace ( "Unable to load requested document." ); |
11 |
function configureListeners(dispatcher:IEventDispatcher): void { |
12 |
dispatcher.addEventListener(Event.COMPLETE, completeHandler); |
13 |
dispatcher.addEventListener(Event.OPEN, openHandler); |
14 |
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler); |
15 |
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); |
16 |
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler); |
17 |
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); |
21 |
function completeHandler(event:Event): void { |
22 |
var loader:URLLoader=URLLoader(event.target); |
23 |
trace ( "completeHandler: " + loader.data); |
24 |
lblReceive.text = loader.data; |
25 |
var var s:URLVariables= new URLVariables(loader.data); |
26 |
trace ( "The Method is " + var s.Method); |
30 |
function openHandler(event:Event): void { |
31 |
trace ( "openHandler: " + event); |
35 |
function progressHandler(event:ProgressEvent): void { |
36 |
trace ( "progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal); |
40 |
function securityErrorHandler(event:SecurityErrorEvent): void { |
41 |
trace ( "securityErrorHandler: " + event); |
45 |
function httpStatusHandler(event:HTTPStatusEvent): void { |
46 |
trace ( "httpStatusHandler: " + event); |
50 |
function ioErrorHandler(event:IOErrorEvent): void { |
51 |
trace ( "ioErrorHandler: " + event); |
服务端FlashHander.ashx可以这样处理:
注意:返回的字符串格式为 name1=value1&name2=value2&name3=value3... 如果name和value中本身包含"="与"&",请注意用其它字符替换掉
02 |
/// Summary description for FlashHander |
04 |
public class FlashHander : IHttpHandler |
06 |
public void ProcessRequest(HttpContext context) |
08 |
context.Response.ContentType = "text/plain" ; |
09 |
context.Response.Write( "msg=Hello World&Method=" + context.Request.HttpMethod + "&q=" + context.Request[ "q" ]); |
11 |
public bool IsReusable |
服务端可以这样处理: