Use URLRequest and Urlloader to interact with the server in FLASH AS3

Source: Internet
Author: User
Tags copy file system urlencode
Request| Server | interactive

Using URLRequest and Urlloader to interact with the server in AS3

The previous version Loadvars method was replaced with Urlloader and URLRequest in AS3. Beginners may be confused again.
In the current Help system is not perfect situation (Flash 9 does not help, Flex Builder 2 only in English), here to write a simple tutorial to facilitate the transition to AS3.

In AS3, there are built-in classes about network operations all in Flash.net. Here is a checklist of the classes to use for this tutorial:

Urlloader: for reading files from the network or locally, you can change the type of text received by setting his DataFormat property.
Unlike AS2 's loadvars, his default value is Urlloaderdataformat.text plain text, so it's time to modify his dataformat when reading an external text variable For Urlloaderdataformat.variables. However, there is no property in AS2, instead of the ContentType property.

urlrequest: used to pass variables to the server and urlloader the target path to load. You can change the type of variable sent to the server by setting his ContentType property, which by default is application/x-form-urlencoding, or UrlEncode encoding.

urlvariables: used to configure key/value collections passed to server variables, such as User1=kakera&user2=eigo.

Urlloaderdataformat: used to set the type of Urlloader read file, with text (plain text), VARIABLES (urlencoding key/value set), BINARY (2), UR Lloader will decode operations based on the appropriate type, such as decoding UrlEncode

At the same time there are other less common:
Urlrequestmethod: decide which way to use to pass data to the server, POST, or get.
Urlreqeustheader: used to configure HTTP headers to be passed to the server.

It is worth mentioning that Urlloader also has quite a complete event for us to get the state of reading the data, and here is a list of the Urlloader events.

Complete: when the Urlloader.load () method is used, the data is triggered when it is completely loaded, and usually if the event is triggered, your program is OK.

Httpstatus: when the Urlloader.load () method is used to get the HTTP status code triggered, we can obtain the loading status of the remote file by judging his state property. Success (200), no permissions (403), no file found (404), Server internal error (500), and so on. This event is always triggered before compelete.

ioerror: When you use the Urlloader.load () method, it triggers a fatal error that I haven't met before.

Open: When you start downloading data from the server after using the Urlloader.load () method, the urlloader.bytesloaded must be 0.

progress: after using the Urlloader.load () method, the download data from the server continues to trigger, by listening to his changes we can easily for the urlloader to do loading status display.

Securityerror: Flashplayer security errors, such as Cross-domain loading, sending/reading data on the server from the hard disk (file system).

Suggestions for registering listeners

All events for Flashplayer built-in classes are represented by related event class constants, and they are recommended for registering event listeners. All of the built-in class event classes (events) are all in the flash.events package, such as using:

Import flash.events.Event;
Urlloaderinstance.addeventlistener (Event.complete, Completehandler);

To replace

Urlloaderinstance.addeventlistener ("complete", Completehandler);

The following is a list of classes for the events supported by Urlloader that can be found in the Flex Builder 2 documentation:

Complete: Event.complete
Httpstatus: Httpstatusevent.http_status
IOError: Ioerrorevent.io_error
Open: Event.open
Progress: Progressevent.progress
Securityerror: Securityerrorevent.security_error

Then here are some simple examples of using Urlloader

1. Simply read the server or the hard drive on the same directory called Variables.txt text file, text content is:

User1=kakera&user2=eigo&user3=keirago

Loadvariablesonly.fla

Code Copy Box
Import Flash.net.urlloader;import Flash.net.urlrequest;import Flash.net.urlloaderdataformat;import Flash.net.urlvariables;import Flash.events.event;import Flash.events.httpstatusevent;import Flash.events.ioerrorevent;import flash.events.progressevent;import flash.events.securityerrorevent;////Configuration URLRequest, set the target path//var request:urlrequest = new URLRequest ("Variables.txt"),////configure Urlloader, register listeners, and so on//var loader: Urlloader = new Urlloader (); Loader.dataformat = Urlloaderdataformat.variables;loader.addeventlistener ( Event.complete, Loader_complete); Loader.addeventlistener (Event.open, Loader_open); Loader.addeventlistener ( Httpstatusevent.http_status, Loader_httpstatus); Loader.addeventlistener (progressevent.progress, loader_progress) ; Loader.addeventlistener (Securityerrorevent.security_error, loader_security); Loader.addeventlistener ( Ioerrorevent.io_error, Loader_ioerror); loader.load (request); function Loader_complete (e:event): void {trace (" Event.complete "); Trace ("Raw data for the target file (plain text):" + Loader.daTA); Use Urlvariables to process raw data and traverse the same time output data//var variables:urlvariables = new Urlvariables (loader.data); for (var i in variables) {Trace (i + ":" + Variables[i]);} function Loader_open (e:event): void {trace ("Event.open"), Trace ("bytes read:" + loader.bytesloaded);} function Loader_httpstatus (e:httpstatusevent): void {trace ("Httpstatusevent.http_status"); Trace ("HTTP Status code:" + E.state);} function Loader_progress (e:progressevent): void {trace ("progressevent.progress"); Trace ("bytes read:" + loader.bytesloaded); Trace ("Total File bytes:" + loader.bytestotal);} function Loader_security (e:securityerrorevent): void {trace ("Securityerrorevent.security_error");} function Loader_ioerror (e:ioerrorevent): void {trace ("Ioerrorevent.io_error");}
[Ctrl + a All select and copy]

2. Submit data to serverside.asp and output the return value of the server

Serverside.asp

Code Copy Box
<% @LANGUAGE = "JSCRIPT" codepage= "65001"%><%///Global Header, set encoding, and cache//with (Response) {Charset = "utf-8"; Buffer = true; Expires =-10; ContentType = "Text/plain"; var buffer = "List variables received by all server: \ n"; var i; var e = new Enumerator (Request.Form); Traversal collection//for (;!e.atend (); E.movenext ()) {i = E.item (); Buffer + + string (i) + ":" + string (Request.Form (i)) + "\ n";} Response.Write (buffer);%>
[Ctrl + a All select and copy]

Sendandloadvariables.fla

Code Copy Box
Import Flash.net.urlloader;import Flash.net.urlrequest;import Flash.net.urlloaderdataformat;import Flash.net.urlvariables;import Flash.events.event;import Flash.events.httpstatusevent;import Flash.events.ioerrorevent;import flash.events.progressevent;import flash.events.securityerrorevent;////Configuration Urlvariables, set the data to be passed to the server//var variables:urlvariables = new Urlvariables (); variables.username = "Kakera"; Variables.password = "********";////configure URLRequest, set target path, set submitted data, Method (post/get)//var = new Request:urlrequest EST ("http://localhost/urlloadersample/ServerSide.asp"); request.data = Variables;request.method = urlrequestmethod.post;////configuration Urlloader, registering listeners, etc.//var Loader:urlloader = new Urlloader (); The////server will return the plain text data// Loader.dataformat = Urlloaderdataformat.text;loader.addeventlistener (Event.complete, loader_complete); Loader.addeventlistener (Event.open, Loader_open); Loader.addeventlistener (Httpstatusevent.http_status, Loader_ Httpstatus); Loader.addeventlistener (progressevent.progresS, loader_progress); Loader.addeventlistener (Securityerrorevent.security_error, loader_security); Loader.addeventlistener (Ioerrorevent.io_error, Loader_ioerror); loader.load (request); function Loader_complete (e: Event): void {trace ("Event.complete"); Trace ("Raw data for the target file (plain text): \ n" + loader.data);} function Loader_open (e:event): void {trace ("Event.open"), Trace ("bytes read:" + loader.bytesloaded);} function Loader_httpstatus (e:httpstatusevent): void {trace ("Httpstatusevent.http_status"); Trace ("HTTP Status code:" + E.status);} function Loader_progress (e:progressevent): void {trace ("progressevent.progress"); Trace ("bytes read:" + loader.bytesloaded); Trace ("Total File bytes:" + loader.bytestotal);} function Loader_security (e:securityerrorevent): void {trace ("Securityerrorevent.security_error");} function Loader_ioerror (e:ioerrorevent): void {trace ("Ioerrorevent.io_error");}
[Ctrl + a All select and copy]

Click here to download the source file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.