REST 样式 Web 服务请求Flash Player 9 和更高版本,Adobe AIR 1.0 和更高版本 REST 样式 Web 服务使用 HTTP 方法动词指定基本动作,并使用 URL 变量指定动作详细信息。例如,请求获得某个项目的数据时可以使用 GET 动词和 URL 变量指定方法名称和项目 ID。生成的 URL 字符串可能是: http://service.example.com/?method=getItem&id=d3452 要使用 ActionScript 访问 REST 样式 Web 服务,您可以使用 URLRequest、URLVariables 和 URLLoader 类。在 AIR 应用程序内的 JavaScript 代码中,您还可以使用 XMLHttpRequest。 在 ActionScript 中编程 REST 样式 Web 服务调用,通常包括下列步骤:
例如,请考虑一种用于公开测试方法的 Web 服务,该方法将调用参数回显给请求者。可使用以下 ActionScript 代码调用此服务: import flash.events.Event; import flash.events.ErrorEvent; import flash.events.IOErrorEvent; import flash.events.SecurityErrorEvent; import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.URLRequestMethod; import flash.net.URLVariables; private var requestor:URLLoader = new URLLoader(); public function restServiceCall():void { //Create the HTTP request object var request:URLRequest = new URLRequest( "http://service.example.com/" ); request.method = URLRequestMethod.GET; //Add the URL variables var variables:URLVariables = new URLVariables(); variables.method = "test.echo"; variables.api_key = "123456ABC"; variables.message = "Able was I, ere I saw Elba."; request.data = variables; //Initiate the transaction requestor = new URLLoader(); requestor.addEventListener( Event.COMPLETE, httpRequestComplete ); requestor.addEventListener( IOErrorEvent.IOERROR, httpRequestError ); requestor.addEventListener( SecurityErrorEvent.SECURITY_ERROR, httpRequestError ); requestor.load( request ); } private function httpRequestComplete( event:Event ):void { trace( event.target.data ); } private function httpRequestError( error:ErrorEvent ):void{ trace( "An error occured: " + error.message ); } 在 AIR 应用程序内的 JavaScript 中,您可以使用 XMLHttpRequest 对象提出相同请求: <html> <head><title>RESTful web service request</title> <script type="text/javascript"> function makeRequest() { var requestDisplay = document.getElementById( "request" ); var resultDisplay = document.getElementById( "result" ); //Create a conveninece object to hold the call properties var request = {}; request.URL = "http://service.example.com/"; request.method = "test.echo"; request.HTTPmethod = "GET"; request.parameters = {}; request.parameters.api_key = "ABCDEF123"; request.parameters.message = "Able was I ere I saw Elba."; var requestURL = makeURL( request ); xmlhttp = new XMLHttpRequest(); xmlhttp.open( request.HTTPmethod, requestURL, true); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4) { resultDisplay.innerHTML = xmlhttp.responseText; } } xmlhttp.send(null); requestDisplay.innerHTML = requestURL; } //Convert the request object into a properly formatted URL function makeURL( request ) { var url = request.URL + "?method=" + escape( request.method ); for( var property in request.parameters ) { url += "&" + property + "=" + escape( request.parameters[property] ); } return url; } </script> </head> <body onload="makeRequest()"> <h1>Request:</h1> <div id="request"></div> <h1>Result:</h1> <div id="result"></div> </body> </html> |
|