First, this is based on the HTTP protocol, and then mainly the following five methods.
/**
* This is based on the HTTP protocol.
* Five methods are introduced in total.
*/
Package
{
Import flash. display. Sprite;
Import flash. events .*;
Import flash.net. URLLoader;
Import flash.net. URLRequest;
Import flash.net. URLVariables;
Import flash.net. URLRequestMethod;
Import flash.net. URLLoaderDataFormat;
Import flash.net. URLRequestHeader;
Import flash. utils. ByteArray;
/**
*...
* @ Author... Li jianstar
* @ Contact .. QQ: 168527720
*/
Public class ASWithPHP_1 extends Sprite
{
Private var urlLoader: URLLoader;
Private var phpUrl: URLRequest;
Public function ASWithPHP_1 ()
{
Init ();
}
Private function init (): void
{
UrlLoader = new URLLoader ();
PhpUrl = new URLRequest ("PHP address ");
/**
* First: directly read the data in PHP
*/
//----------------------------------------------------------------------
UrlLoader. dataFormat = URLLoaderDataFormat. VARIABLES;
/**
* URLLoaderDataFormat. BINARY: String ----> specifies that the downloaded data is received in the original BINARY data format.
* URLLoaderDataFormat. TEXT: String ----> specify to receive downloaded data in TEXT format
* URLLoaderDataFormat. VARIABLES: String ----> specify to receive downloaded data in the form of URL encoded VARIABLES.
*/
UrlLoader. load (phpUrl );
UrlLoader. addEventListener (Event. COMPLETE, completeHandler1 );
//-----------------------------------------------------------------------
/**
* Type 2: Read the xml generated by PHP
*/
//-------------------------------------------------------------------------
UrlLoader. load (phpUrl );
UrlLoader. addEventListener (Event. COMPLETE, completeHandler2 );
//-------------------------------------------------------------------------
/**
* Method 3: Pass the parameter to PHP using the GET method.
*/
//--------------------------------------------------------------------------
PhpUrl. method = URLRequestMethod. GET;
PhpUrl. data = "data to be transmitted ";
UrlLoader. load (phpUrl );
UrlLoader. addEventListener (Event. COMPLETE, completeHandler3 );
//--------------------------------------------------------------------------
/**
* Method 4: Upload the parameter to www.2cto.com in PHP using the POST method.
*/
//--------------------------------------------------------------------------
PhpUrl. method = URLRequestMethod. POST;
Var vars: URLVariables = new URLVariables ();
Vars. value1 = "parameter 1 ";
Vars. value2 = "parameter 2 ";
PhpUrl. data = vars;
UrlLoader. dataFormat = URLLoaderDataFormat. VARIABLES;
UrlLoader. load (phpUrl );
UrlLoader. addEventListener (Event. COMPLETE, completeHandler4 );
//--------------------------------------------------------------------------
/**
* Method 5: Binary Communication
*/
//----------------------------------------------------------------------------
Var requestHeader: URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream ");
PhpUrl. method = URLRequestMethod. POST;
PhpUrl. requestHeaders. push (requestHeader );
Var byteArr: ByteArray = new ByteArray ();
ByteArr. writeByte (12 );
ByteArr. writeUTF ("CNSloppyMan ");
Var sendData: ByteArray = new ByteArray ();
SendData. writeInt (byteArr. length );
SendData. writeBytes (byteArr );
PhpUrl. data = sendData;
UrlLoader. dataFormat = URLLoaderDataFormat. BINARY;
UrlLoader. load (phpUrl );
UrlLoader. addEventListener (Event. COMPLETE, completeHandler5 );
//-----------------------------------------------------------------------------
}
Private function completeHandler1 (e: Event): void
{
Var vars: URLVariables = URLVariables (e. currentTarget as URLLoader). data );
Trace ("Accept data:" + vars. value); // assume that value is the custom node value in PHP.
}
Private function completeHandler2 (e: Event): void {
Var xml: XML = new XML (e. currentTarget as URLLoader). data );
Trace ("xml data:" + xml. toString ());
}
Private function completeHandler3 (e: Event): void {
Trace ("GET-Outgoing data:" + (e. currentTarget as URLLoader). data );
}
Private function completeHandler4 (e: Event): void {
Trace ("POST-Outgoing data:" + (e. currentTarget as URLLoader). data );
}
Private function completeHandler5 (e: Event): void {
Var _ byteArr: ByteArray = e. currentTarget. data as ByteArray;
Trace (_ byteArr. readInt (); // 14
Trace (_ byteArr. readByte (); // 12
Trace (_ byteArr. readUTF (); // CNSloppyMan
}
}
}
From Li xinxin's column