We need to write a piece of code that sends some ( XML ?) message through an HTTP request to some servlet, get the HTTP response and extract the reply XML message from the response.
First, create an instance of the URLLoader class, The URLLoader class downloads data from a URL as text (default), binary data, or URL-encoded variables.
var requestSender:URLLoader = new URLLoader();
Second, add event handler for the complete event of the loader class:
requestSender.addEventListener(Event.COMPLETE, completeHandler);
We then have to create a URLRequest instance, taking the URL of the servlet. The URLRequest class captures all of the information in a single HTTP request.
var urlRequest :URLRequest = new URLRequest("serverUrl");
Next, we create the message to be sent and put it in the "data" property of the URLRequest instance created above, set the value of the URLRequest.method property to URLRequestMethod.POST.
var msg:String = “valid XML msg” ;
urlRequest.data = msg;
urlRequest.set_method("POST");
After that, the request object is then passed to URLLoader.load(), which does the HTTP request and posts the message:
requestSender.load(urlRequest);
When the URLLoader has finished loading the text file the Event.COMPLETE event fires, triggering the completeHandler() method.
function completeHandler(event:Event)
{
var response:URLLoader = URLLoader(event.target);
String responseData = this.response.data;
}
Note that the reply XML message is obtained from the URLoader.data property on the instance obtained from the event.target.
We now have that working piece of code:
var requestSender:URLLoader = new URLLoader();
var urlRequest :URLRequest = new URLRequest("serverUrl");
var msg:String = “valid XML msg” ;
requestSender.addEventListener(Event.COMPLETE, completeHandler);
urlRequest.data = msg;
urlRequest.set_method("POST");
requestSender.load(urlRequest);
function completeHandler(event:Event)
{
var response:URLLoader = URLLoader(event.target);
String responseData = this.response.data;
}
As you might already have guessed, the responseData can be then used to construct an XMLDocument instance, where you can perform different XML parsing operations.