Функция:
/* Function to make a generic HTTP Request
* loc = location e.g. http://www.google.com
* req = request
* type = content type
* complete = function to execute when HTTP req returns with complete
*/
public function httpReq(loc:String , req:String, type:String, complete:Function):void{
/* Start the HTTP Request */
try{
requestSender= new URLLoader();
requestSender.addEventListener(Event.COMPLETE, complete);
requestSender.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, printResponse);
var urlRequest :URLRequest = new URLRequest(loc);
/* Setup HTTP Request */
urlRequest.data = req;
urlRequest.contentType = type;
urlRequest.method = URLRequestMethod.GET;
requestSender.load(urlRequest); // Send the request off, when complete jump to completeHandler
}
catch(e:Error){
trace(e.message);
trace(e.getStackTrace());
}
}
Получить ответ:
/* HTTP Request complete, back with response */
private function responseHandler(event:Event):void
{
this.requestSender = URLLoader(event.target); // Set requestSender as response
this.output.text = this.requestSender.data; // Get its data
trace("THE DATA IS "+this.requestSender.data);
this.requestSender.close();
alert.cancel();
}
Пример вызова:
httpReq("http://www.google.com" , "requestString", "application/x-www-form-urlencoded", responseHandler);