In Flash application development, it is often necessary to use the communication with the Web server side, to store the current application state on the server or to obtain information from the server side, etc. For Flash, he has provided support for the HTTP protocol and the network socket (socket). For the HTTP protocol support, flash through URLRequest, URLLoader, Urlvariables and other classes to complete the operation.
1. Get external images and other resources in Flash
In ACTIONSCRIPT3, the acquisition of external resources is accomplished primarily through the urlrequest and Urlloader two classes, which can be monitored to get the state of external resources by registering events such as complete and progress of the Urlloader class.
var myloader:urlloader=new URLLoader () var myxmllocation:urlrequest = new URLRequest ("Test.xml"); Myloader.load ( myxmllocation); MyLoader.contentLoaderInfo.addEventListener (Event.complete,xmlloadcomplete);
If the external resources we get are in binary form, we need to use the loader class instead of the Urlloader class.
2. The information in Flash is transmitted to the Web server
In flash need to pass data to the Web server, we can only through post or get, post action relative to get can pass more data, the specific difference is the HTTP entity body, for post, the value sent is in the entity body , the entity body is always empty for get. For an HTML form, you can indicate in the form's method whether the current pass data is post or get, but in Flash, you need to indicate whether the current operation is post or get in the urlrequest pair image. In Flash, when you need to pass data to a Web server, you need to urlrequest,urlvariables,urlloader three classes to complete.
var values:urlvariables =new urlvariables (); Values.key= "Message"; var header:urlrequestheader = new Urlrequestheader ("Pragma", "no-cache"); var request:urlrequest=new urlrequest ("http://localhost/shopguide/data.php"); Request.data=values; Request.requestHeaders.push (header); Request.method=urlrequestmethod.post; var loader:urlloader=new URLLoader (); Try { loader.load (request); } catch (Error:argumenterror) { trace ("An argumenterror has occurred."); catch (Error:securityerror) { trace ("A securityerror has occurred.");
The above code uses Urlvariables to construct a key-value pair to pass the data, so that the data passed can be obtained by $_post["key" on the PHP side. The Urlrequestheadere in the code is an HTTP request header that should be forwarded to the original server, even if the application has a cached copy of the requested content. The following figure is the request information that you see under the Developer tool for IE9.
On the PHP server, we only need echo ($_post[' key '), we can get the requested information, and the response body of the third picture is the echo output in PHP.
3. Use Php://input to get the contents of a flash request without specifying a key-value pair
In the book "ACTIONSCRIPT 3.0 Programming", he gives an example of an XML-to-Web server that does not use the Urlvariables class to construct a key-value pair, as shown in the following example code:
var secondsutc:number = new Date (). Time;var dataxml:xml =
{SECONDSUTC}
Ernie
guru
var
request:urlrequest = new URLRequest ("Http://localhost/shopguide /data.php "); request.contenttype =" Text/xml "; request.data = Dataxml.toxmlstring (); Request.method = Urlrequestmethod.post;var Loader:urlloader = new URLLoader (); try{ loader.load (request);} catch (Error:argumenterror) { trace ("An argumenterror has occurred."); catch (Error:securityerror) { trace ("A securityerror has occurred.");
The difference between the request and the use of the Urlvariables class is that the contents of the request are sent directly, that is, there is no key value, the content of the request is as follows:
How does a request like this get data in PHP? Later check the information, can be obtained through PHP file_get_contents ("Php://input"). In PHP, File_get_contents returns a string. By outputting the contents of the file_get_contents we can see the following response body.
For Php://input, please refer to this article:
What does php://input mean? Introduction to PHP input stream input
The socket class and the Xmlsocket class are also available in ACTIONSSCRIPT3, which can be used in the book "ACTIONSCRIPT 3.0 Programming".