So I am working on this project where I need to consume a .NET web service. In Flash 8 it was cool because I could use the (built in) web services class. But for this project I am using Flash 9 / as3, and so there are no built-in classes for handling web services.
I am not using Flex for this project. Flex is not an option.
Also, I am kind of new to the whole Web Services thing. If I am overlooking something easy, please let me know.
kthxbi
-eS?
es! 2007.05.31, 02:03PM — netconnection sending and receiving soap...as3 WITHOUT using Flex???
es! 2007.05.31, 07:38PM —
never mind...sorted it out...
Here is a code snippet:
package {
import flash.net.*;
import flash.xml.XMLDocument;
import flash.events.Event;
import flash.events.ErrorEvent;
public class MyWebService {
public var feedURL:String;
public var returnXML:XML;
public var isLoaded:Boolean = false;
private var urlLoader:URLLoader;
public var soapXML:XML;
public var dataXML:XML;
public var soap12:Namespace = new Namespace("http://www.w3.org/2003/05/soap-envelope");
public var xsi:Namespace = new Namespace("http://www.w3.org/2001/XMLSchema-instance");
public var xsd:Namespace = new Namespace("http://www.w3.org/2001/XMLSchema");
public function MyWebService() {}
public function loadService($feedURL:String):void {
isLoaded = false;
feedURL = $feedURL;
var urlRequest:URLRequest = new URLRequest(feedURL);
urlRequest.method=URLRequestMethod.POST;
urlRequest.requestHeaders.push(new URLRequestHeader("Content-Type", "application/soap+xml"));
urlRequest.data = null /* put SOAP request XML here */;
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener("complete", onLoaded);
urlLoader.addEventListener("ioerror", ifFailed);
urlLoader.load(urlRequest);
}
public function onLoaded():void {trace("IT WORKEDED")}
public function ifFailed():void {trace("IT FAILEDED");}
}
}
call it as follows:
var fnord.MyWebService = new MyWebService();
fnord.loadService("http://www.myWebServiceURL.cx");
lithium 2007.06.01, 12:33AM —
if you're consuming soap web services, you might want to set your urlLoader.dataFormat to "e4x" so you can use the nifty new xml parsing to process the results.
es! 2007.06.01, 01:22AM —
I didn't have any difficulty parsing it as is (I don't think...) but I will do as you say. Thanks for the tip!
monoloco 2007.07.25, 04:20AM —
I'm really new to Web Services and am trying to get them to work in AS3.0. I might be missing something here, but looking at the code snippet, I'm still confused as to how the Namespace objects get included into the SOAP request. Also, where does the method name of the method being called get included?
Maybe all this stuff happens when you replace null with your SOAP in the below line from your example
urlRequest.data = null /* put SOAP request XML here */;
Any help welcomed!