Flash/Filex学習ノート(4):WebページおよびGet/Postデータを開く方法
4465 ワード
Flashはあくまでクライアント技術であるため、サービス側技術(例えばasp,asp.net,jsp,phpなど)とデータインタラクションを行う必要がある場合が多い.以下のコードは、flashでWebページを開く方法と、GET/POSTの2つの方法でサービス側にデータを送信する方法を示している.
サービス側は、次のように処理できます.
サービス側ashxは、次のように処理できます.
注意:返される文字列のフォーマットはname 1=value 1&name 2=value 2&name 3=value 3...nameとvalue自体に「=」と「&」が含まれている場合は、他の文字で置き換えることに注意してください.
// ,
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);
}
サービス側ashxは、次のように処理できます.
注意:返される文字列のフォーマットはname 1=value 1&name 2=value 2&name 3=value 3...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;
}
}
}