April 27, 2009 | In: Flash, Work

Actionscript 3 and SOAP Webservices

I just finished a project where my AS3 Game needs to connect to to a third party .NET web service. It turned out more complicated then I had anticipated, especially since I was asked to dispatch soap package to the web service.  Thank god for the community, because, off course, a workaround has already been done.

First off, by some reason, Flash does not like namespaces, nor any <soap:something> formulated XML. So to fix  any SOAP XML loaded into XML this Class comes in very handy. AS3:Namespace Remover

Secondly, to actually connect to a webservice, I am using this Class, from the same developer.gofr available for download.

So big ups to Alducente Labs, for all that very valuable sharing…

3 Responses to Actionscript 3 and SOAP Webservices

Avatar

Jason

March 24th, 2010 at 12:34

Suggested reading:
Advanced ActionScript 3 with Design Patterns
by Joey Lott and Danny Patterson
Go to chapter 15 for a better and complete solution.

My example code:
private var xmlResults:XML = new URLLoader;
private var urlLoader:URLLoader = new URLLoader();

public function fErrorHandler(e:IOErrorEvent):void {
trace(“xml failed!”);
}

//when the “loader” event object finishes the loading of the xml file, run other needed functions to process the data
private function fLoaderCompleteHandler(event:Event):void {

trace(“xml loaded!!”); //jwt testing
var result2:XML = new XML(urlLoader.data); // loads the xml file data into the xml object
xmlResults = result2; //have the global xml object equal the current data loaded xml object
result2 = null; // dont need current xml object any more so erase it

fParseXML(0); //process the first set of records from the xml object
trace(“found xml” + xmlResults.toString());
}

public function fParseXML(intAddNum:Number):void{
//create variables to store the record info
//
var envelope:XML = xmlResults; //XML(urlLoader.data);
var soap:Namespace = envelope.namespace(“soap”);
var responseN:Namespace = new Namespace(“http://www.domain.com/school/ServiceCollege”);
var resultL:XMLList = envelope.soap::Body.responseN::HelloTestResponse.responseN::HelloTestResult;

var strTest:String;
//if only one response field will be return by the web service
strTest = resultL;
//do something like this if multiple response feilds will be returned
//strTest = resultL.response::schoolName.toString();
trace(“parsing:” + strTest);
trace(“done loading:”);
}

public function fBeginRequest():void{
var strXMLDataRequest:String
var urlRequest:URLRequest = new URLRequest();
urlRequest.contentType = “text/xml; charset=utf-8″;
urlRequest.method = “POST”;
urlRequest.url = “http://www.domain.com/school/serviceCollege.asmx”;
var soapAction:URLRequestHeader = new URLRequestHeader(“SOAPAction”,”http://www.domain.com/school/ServiceCollege/HelloTest”);
urlRequest.requestHeaders.push(soapAction);
strXMLDataRequest = “<soap:Envelope xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\” xmlns:xsd=\”http://www.w3.org/2001/XMLSchema\” xmlns:soap=\”http://schemas.xmlsoap.org/soap/envelope/\”>”;
strXMLDataRequest = strXMLDataRequest + “<soap:Body>”;
strXMLDataRequest = strXMLDataRequest + “<HelloTest xmlns=\”http://www.domain.com/school/ServiceCollege\” />”;
strXMLDataRequest = strXMLDataRequest + “</soap:Body>”;
strXMLDataRequest = strXMLDataRequest + “</soap:Envelope>”;
urlRequest.data = new XML(strXMLDataRequest);
urlLoader.load(urlRequest);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, fErrorHandler); //add the event to the “loader” object
urlLoader.addEventListener(Event.COMPLETE, fLoaderCompleteHandler); //add the event to the “loader” object
}

Soap Request:
<?xml version=”1.0″ encoding=”utf-8″?>
<soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”>
<soap:Body>
<HelloTest xmlns=”http://www.domain.com/school/ServiceCollege” />
</soap:Body>
</soap:Envelope>

Soap Response:
<?xml version=”1.0″ encoding=”utf-8″?>
<soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”>
<soap:Body>
<HelloTestResponse xmlns=”http://www.domain.com/school/ServiceCollege”>
<HelloTestResult>string
</HelloTestResponse>
</soap:Body>
</soap:Envelope>

Avatar

vevmesteren

March 24th, 2010 at 12:37

Awesome thanks my own favorite Design Pattern book : http://www.as3dp.com/ the site for this book

Avatar

Noel

April 19th, 2010 at 8:59

Hello, looking to communicate with a web service and found this site. Very useful but how do you send an XML SOAP request to the web service?

Comment Form

  • It would be awfully nice of you, if you could spare me with a click below.

 

Categories

Posts

September 2010
M T W T F S S
« Aug    
 12345
6789101112
13141516171819
20212223242526
27282930