Flash is only a client technology after all, so it is often necessary to interact with server technologies (such as ASP, Asp.net, JSP, PHP, and so on). The following Code Demonstrate how to open a webpage in flash and send data to the server using get/post
// Press the button to open the webpage btnopen. addeventlistener (mouseevent. Click, function () {navigatetoURL (New URLRequest ("http://www.g.cn/search? Hl = ZH-CN & Q = "+ encodeuricomponent (txtid. text), "_ blank");}); // send data in get mode (send is complete, ignore whether the server responds) btnsend. addeventlistener (mouseevent. click, function () {sendtourl (New URLRequest ("/default. aspx? Q = "+ encodeuricomponent (txtid. text);}); btnpost. addeventlistener (mouseevent. click, fnpostdata); // send data in post mode (similarly, sending is done, and does not care whether the server responds) function fnpostdata (E: mouseevent) {VaR _ urlreq: URLRequest = new URLRequest (); _ urlreq. url = "/default. aspx "; _ urlreq. method = urlrequestmethod. post; VaR _ DATA: urlvariables = new urlvariables (); _ data. Q = "Yang Guo under the bodhi tree"; // transfer q = Yang Guo under the bodhi tree. Note: after testing, flash will automatically encodeuricomponent the transmitted data, therefore, you cannot add encodeuricomponent. Otherwise, it is a second encoded _ urlreq. data = _ data; sendtourl (_ urlreq );}
The server can handle the problem as follows:
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 );}}
If you want to respond to the results of the server after sending the data (for example, obtain the return value of the server and then process it in Flash), you can write the following in flash:
VaR Loader: urlloader = new urlloader (); configurelisteners (loader); var request: URLRequest = new URLRequest ("/flashhander. ashx? Q = "+ encodeuricomponent (" Yang Guo under the bodhi tree "); try {loader. load (request);} catch (error: Error) {trace ("unable to load requested document. ");} // define the callback 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);} // when loading is complete, function completehandler (Event: Event): void {var Loader: urlloader+urlloader(event.tar get) is triggered; trace ("completehandler:" + loader. data); lblreceive. TEXT = loader. data; // In this example, the server returns: MSG = Hello World & method = get & Q = Yang Guo var vars: urlvariables = new urlvariables (loader. data); trace ("the method is" + vars. method); // If the string returned by the server contains a character such as method = xxx, vars can be directly used in flash. method access} // when the request is initiated, function openhandler (Event: Event): void {trace ("openhandler:" + Event) will be triggered );} // when the download progress changes, the function progresshandler (Event: progressevent): void {trace ("progresshandler loaded:" + event. bytesloaded + "Total:" + event. bytestotal);} // when an error occurs due to security reasons, function securityerrorhandler (Event: securityerrorevent): void {trace ("securityerrorhandler:" + Event) will be triggered );} // when the HTTP Request status changes, the function httpstatushandler (Event: httpstatusevent): void {trace ("httpstatushandler:" + Event);} // Io error is triggered, function ioerrorhandler (Event: ioerrorevent) will be triggered: void {trace ("ioerrorhandler:" + Event );}
The server flashhander. ashx can be processed as follows:
Note: The returned string format is name1 = value1 & name2 = value2 & name3 = value3... if the name and value contain "=" and "&", replace them with other characters.
////// 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 ;}}}