AS3與PHP通訊的五種方法(基於HTTP協議)

來源:互聯網
上載者:User

基於HTTP協議的,然後主要是下面的五種方法。

 

 

  1 /**  2  * 這是基於HTTP協議的  3  * 一共介紹了五種方法  4  */  5   6 package    7 {  8     import flash.display.Sprite;  9     import flash.events.*; 10     import flash.net.URLLoader; 11     import flash.net.URLRequest; 12     import flash.net.URLVariables; 13     import flash.net.URLRequestMethod; 14     import flash.net.URLLoaderDataFormat; 15     import flash.net.URLRequestHeader; 16     import flash.utils.ByteArray; 17     /** 18      * ... 19      *  20      *  21      */ 22      23     public class ASWithPHP_1 extends Sprite  24     { 25         private var urlLoader:URLLoader; 26         private var phpUrl:URLRequest; 27          28         public function ASWithPHP_1()  29         { 30             init(); 31         } 32          33         private function init():void  34         { 35             urlLoader = new URLLoader(); 36             phpUrl = new URLRequest("PHP地址"); 37              38             /** 39              * 第一種:直接讀取PHP裡面的資料 40              */ 41             //---------------------------------------------------------------------- 42             urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES; 43             /** 44              * URLLoaderDataFormat.BINARY:String---->指定以原始位元據形式接收下載的資料 45              * URLLoaderDataFormat.TEXT:String----> 指定以文本形式接收已下載的資料 46              * URLLoaderDataFormat.VARIABLES:String---->指定以 URL 編碼變數形式接收下載的資料  47              */ 48             urlLoader.load(phpUrl); 49             urlLoader.addEventListener(Event.COMPLETE, completeHandler1); 50             //----------------------------------------------------------------------- 51              52             /** 53              * 第二種:讀取PHP產生的xml  54              */ 55             //------------------------------------------------------------------------- 56             urlLoader.load(phpUrl); 57             urlLoader.addEventListener(Event.COMPLETE, completeHandler2); 58             //------------------------------------------------------------------------- 59              60             /** 61              * 第三種:通過GET方法把參數傳到PHP裡面 62              */ 63             //-------------------------------------------------------------------------- 64             phpUrl.method = URLRequestMethod.GET; 65             phpUrl.data = "要傳出去的資料"; 66             urlLoader.load(phpUrl); 67             urlLoader.addEventListener(Event.COMPLETE, completeHandler3); 68             //-------------------------------------------------------------------------- 69              70             /** 71              * 第四種:通過POST方法把參數傳到PHP裡面 72              */ 73             //-------------------------------------------------------------------------- 74             phpUrl.method = URLRequestMethod.POST; 75             var vars:URLVariables = new URLVariables(); 76             vars.value1 = "參數1"; 77             vars.value2 = "參數2"; 78             phpUrl.data = vars; 79             urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES; 80             urlLoader.load(phpUrl); 81             urlLoader.addEventListener(Event.COMPLETE, completeHandler4); 82             //-------------------------------------------------------------------------- 83              84             /** 85              * 第五種:二進位通訊方法 86              */ 87             //---------------------------------------------------------------------------- 88             var requestHeader:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream"); 89             phpUrl.method = URLRequestMethod.POST; 90             phpUrl.requestHeaders.push(requestHeader); 91             var byteArr:ByteArray = new ByteArray(); 92             byteArr.writeByte(12); 93             byteArr.writeUTF("CNSloppyMan"); 94             var sendData:ByteArray = new ByteArray(); 95             sendData.writeInt(byteArr.length); 96             sendData.writeBytes(byteArr); 97             phpUrl.data = sendData; 98             urlLoader.dataFormat = URLLoaderDataFormat.BINARY; 99             urlLoader.load(phpUrl);100             urlLoader.addEventListener(Event.COMPLETE, completeHandler5);101             //-----------------------------------------------------------------------------102         }103         104         private function completeHandler1(e:Event):void 105         {106             var vars:URLVariables = URLVariables((e.currentTarget as URLLoader).data);107             trace("接受資料:" + vars.value); //假定value是PHP裡面自訂的節點值108         }109         110         private function completeHandler2(e:Event):void {111             var xml:XML = new XML((e.currentTarget as URLLoader).data);112             trace("xml資料:" + xml.toString());113         }114         115         private function completeHandler3(e:Event):void {116             trace("GET-已經傳出去的資料:" + (e.currentTarget as URLLoader).data);    117         }118         119         private function completeHandler4(e:Event):void {120             trace("POST-已經傳出去的資料:" + (e.currentTarget as URLLoader).data);    121         }122         123         private function completeHandler5(e:Event):void {124             var _byteArr:ByteArray = e.currentTarget.data as ByteArray;125             trace(_byteArr.readInt()); //14126             trace(_byteArr.readByte()); //12127             trace(_byteArr.readUTF()); //CNSloppyMan128         }129     }130 131 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.