Flex http請求

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   資料   

下面類支援POST和GET請求,請求資料和出錯將返回

 

package com.sole.util{    import flash.events.Event;    import flash.events.HTTPStatusEvent;    import flash.events.IEventDispatcher;    import flash.events.IOErrorEvent;    import flash.events.ProgressEvent;    import flash.events.SecurityErrorEvent;    import flash.net.URLLoader;    import flash.net.URLLoaderDataFormat;    import flash.net.URLRequest;    import flash.net.URLRequestMethod;    import flash.net.URLVariables;      public class HttpConnent    {        /**請求的url*/          public var url:String;        /**請求的參數*/          public var urlVariables:URLVariables;  //        public var resultStr:String;          public var inputstream:Object;                private var urlLoader:URLLoader;          private var request:URLRequest;                  public var completeFun:Function;        public var errFun:Function;                /**         *串連提交請求         * @method 請求類型         * post或get  預設post         * @isstream 是否資料流請求         */        public function gotoConn(method:String=URLRequestMethod.POST,isstream:Boolean=false):void{              urlLoader = new URLLoader();              //URLLoader 類以文本、位元據或 URL 編碼變數的形式從 URL 下載資料              request= new URLRequest(url);               request.method = method;//佈建要求的類型             if(isstream){                request.contentType= "application/octet-stream";                  request.data=inputstream;                urlLoader.dataFormat = URLLoaderDataFormat.BINARY;              }            if(urlVariables)                request.data = urlVariables;//將url請求的資料放入request中                             configureListeners(urlLoader);//給urlloader對象添加監聽事件               try {                  urlLoader.load(request);//開始發送請求               } catch (error:Error) {                  trace(error);                  urlLoader.close();            }         }                  private function configureListeners(dispatcher:IEventDispatcher):void {                //載入完成事件;                 dispatcher.addEventListener(Event.COMPLETE, loaderHandler);                //開始訪問事件;                 dispatcher.addEventListener(Event.OPEN, loaderHandler);                //載入進度事件;                 dispatcher.addEventListener(ProgressEvent.PROGRESS, loaderHandler);                //跨域訪問安全性原則事件;                 dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loaderHandler);                //Http狀態事件;                 dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, loaderHandler);                //訪問出錯事件;                 dispatcher.addEventListener(IOErrorEvent.IO_ERROR, loaderHandler);           }           private function loaderHandler(event:*):void           {               switch(event.type) {                   case Event.COMPLETE:                       trace("成功: ");                      if(completeFun!=null){                        completeFun.call(null,event);                    }                    break;                   case Event.OPEN:                       trace("open: " + event);                       break;                   case ProgressEvent.PROGRESS:                       trace("progress: " + event);                       break;                   case SecurityErrorEvent.SECURITY_ERROR:                       trace("securityError: " + event);                      if(errFun!=null){                        errFun.call(null);                    }                    break;                   case HTTPStatusEvent.HTTP_STATUS:                       trace("httpStatus: " + event);                       break;                   case IOErrorEvent.IO_ERROR:                       trace("ioError: " + event);                       if(errFun!=null){                        errFun.call(null);                    }                    break;                               }          }      }}
View Code

 

 調用該方法

var url:String="";                var variables:URLVariables = new URLVariables();                              variables.ID = UserID;                                var http:HttpConnent=new HttpConnent();                http.url=url;                http.urlVariables=variables;                http.completeFun=customerNumberFun;                http.errFun=customerNumberErrFun;                http.gotoConn(URLRequestMethod.GET);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.