代码
package Charlotte {
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
public class KitaWebServices{
//
private var url:String = new String();
private var ns:String = new String();
private var soap:Namespace = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");
private var soapXML:XML = <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/>
</soap:Envelope>
;
private var elementXML:XMLList;
private var ready:Boolean = false;
private var cache:Array = new Array();
private var shareObj:Object = new Object();
public function KitaWebServices(_url:String)
{
url = _url;
analyseWSDL();
}
private function analyseWSDL():void
{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
var rqa:URLRequest = new URLRequest();
rqa.url = url+"?wsdl";
rqa.method = URLRequestMethod.POST;
loader.addEventListener(Event.COMPLETE,analyseComlete);
loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
loader.load(rqa);
}
private function errorHandler(evt:IOErrorEvent):void
{
trace(evt.type);
}
private function analyseComlete(evt:Event):void
{
var loader:URLLoader = evt.target as URLLoader;
//trace(loader.data);
var temp0:XML = XML(loader.data);
var wsdl:Namespace = temp0.namespace();
var s:Namespace;
for (var i:uint = 0; i < temp0.namespaceDeclarations().length; i++) {
s = temp0.namespaceDeclarations()[i];
var prefix:String = s.prefix;
if (prefix == "s") {
//trace(s.uri);
break;
}
//trace(prefix + ":" , s.uri);
}
//运行时解除!
elementXML = temp0.wsdl::["types"].s::["schema"];
//trace(elementXML);
ns = elementXML.@targetNamespace;
var len:uint = elementXML.s::element.length();
var item:String = new String();
var itemLen:uint = 0;
var j:uint = new uint();
for (i =0;i<len;i+=2)
{
item = elementXML.s::element[i].@name;
shareObj[item] = new Array();
itemLen = elementXML.s::element[i].s::complexType.s::sequence.s::element.length();
for (j =0;j<itemLen;j++)
{
shareObj[item].push(elementXML.s::element[i].s::complexType.s::sequence.s::element[j].@name);
}
//trace(shareObj[item]);
//trace(itemLen);
//
}
ready = true;
for each(var cacheLoader:WebServicesLoader in cache){
cacheLoader.active(ns,shareObj[cacheLoader.method]);
}
cache = new Array();
}
public function call(_method:String,_returnMethod:Function,...args:Array):void
{
var loader:WebServicesLoader = new WebServicesLoader(url,soapXML,_method,_returnMethod,args);
if(ready){
loader.active(ns,shareObj[_method]);
}else
{
cache.push(loader);
}
}
}
}
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
class WebServicesLoader extends URLLoader
{
private var soap:Namespace = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");
private var rq:URLRequest = new URLRequest();
private var callBack:Function;
private var url:String;
private var soapXML:XML;
public var method:String;
private var args:Array;
function WebServicesLoader(_url:String,_soapXML:XML,_method:String,_returnMethod:Function,_args:Array)
{
url = _url;
soapXML = XML(_soapXML.toString());
method = _method;
args = _args;
dataFormat = URLLoaderDataFormat.TEXT;
callBack = _returnMethod;
}
function active(ns:String,argLabels:Array):void
{
//trace(argLabels);
var methodXML :XML = XML("<"+method+" xmlns=\""+ns+"\"/>");
var len:uint = argLabels.length;
for (var i:uint =0;i <len;i++)
{
methodXML.appendChild(XML("<"+argLabels[i]+">"+args[i]+"</"+argLabels[i]+">"));
}
var tempXML:XML = soapXML;
//运行时解除!
tempXML.soap::["Body"].appendChild(methodXML);
var nns:String = ns;
if(nns.lastIndexOf("/")==nns.length-1){
nns = nns.substr(0,nns.length-1);
}
rq.url = url+"?op="+method;
rq.method = URLRequestMethod.POST;
rq.requestHeaders.push(new URLRequestHeader("Content-Type", "text/xml;charset=utf-8"));
rq.requestHeaders.push(new URLRequestHeader("SOAPAction", nns+"/"+method));
rq.data =tempXML;
addEventListener(Event.COMPLETE,comleteHandler);
load(rq);
}
function comleteHandler(evt:Event):void
{
var xml:XML = XML(data);
if(callBack!=null){
callBack.apply(null,new Array(xml.soap::Body.child(0).child(0)));
}
}
}
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
public class KitaWebServices{
//
private var url:String = new String();
private var ns:String = new String();
private var soap:Namespace = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");
private var soapXML:XML = <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/>
</soap:Envelope>
;
private var elementXML:XMLList;
private var ready:Boolean = false;
private var cache:Array = new Array();
private var shareObj:Object = new Object();
public function KitaWebServices(_url:String)
{
url = _url;
analyseWSDL();
}
private function analyseWSDL():void
{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
var rqa:URLRequest = new URLRequest();
rqa.url = url+"?wsdl";
rqa.method = URLRequestMethod.POST;
loader.addEventListener(Event.COMPLETE,analyseComlete);
loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
loader.load(rqa);
}
private function errorHandler(evt:IOErrorEvent):void
{
trace(evt.type);
}
private function analyseComlete(evt:Event):void
{
var loader:URLLoader = evt.target as URLLoader;
//trace(loader.data);
var temp0:XML = XML(loader.data);
var wsdl:Namespace = temp0.namespace();
var s:Namespace;
for (var i:uint = 0; i < temp0.namespaceDeclarations().length; i++) {
s = temp0.namespaceDeclarations()[i];
var prefix:String = s.prefix;
if (prefix == "s") {
//trace(s.uri);
break;
}
//trace(prefix + ":" , s.uri);
}
//运行时解除!
elementXML = temp0.wsdl::["types"].s::["schema"];
//trace(elementXML);
ns = elementXML.@targetNamespace;
var len:uint = elementXML.s::element.length();
var item:String = new String();
var itemLen:uint = 0;
var j:uint = new uint();
for (i =0;i<len;i+=2)
{
item = elementXML.s::element[i].@name;
shareObj[item] = new Array();
itemLen = elementXML.s::element[i].s::complexType.s::sequence.s::element.length();
for (j =0;j<itemLen;j++)
{
shareObj[item].push(elementXML.s::element[i].s::complexType.s::sequence.s::element[j].@name);
}
//trace(shareObj[item]);
//trace(itemLen);
//
}
ready = true;
for each(var cacheLoader:WebServicesLoader in cache){
cacheLoader.active(ns,shareObj[cacheLoader.method]);
}
cache = new Array();
}
public function call(_method:String,_returnMethod:Function,...args:Array):void
{
var loader:WebServicesLoader = new WebServicesLoader(url,soapXML,_method,_returnMethod,args);
if(ready){
loader.active(ns,shareObj[_method]);
}else
{
cache.push(loader);
}
}
}
}
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
class WebServicesLoader extends URLLoader
{
private var soap:Namespace = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");
private var rq:URLRequest = new URLRequest();
private var callBack:Function;
private var url:String;
private var soapXML:XML;
public var method:String;
private var args:Array;
function WebServicesLoader(_url:String,_soapXML:XML,_method:String,_returnMethod:Function,_args:Array)
{
url = _url;
soapXML = XML(_soapXML.toString());
method = _method;
args = _args;
dataFormat = URLLoaderDataFormat.TEXT;
callBack = _returnMethod;
}
function active(ns:String,argLabels:Array):void
{
//trace(argLabels);
var methodXML :XML = XML("<"+method+" xmlns=\""+ns+"\"/>");
var len:uint = argLabels.length;
for (var i:uint =0;i <len;i++)
{
methodXML.appendChild(XML("<"+argLabels[i]+">"+args[i]+"</"+argLabels[i]+">"));
}
var tempXML:XML = soapXML;
//运行时解除!
tempXML.soap::["Body"].appendChild(methodXML);
var nns:String = ns;
if(nns.lastIndexOf("/")==nns.length-1){
nns = nns.substr(0,nns.length-1);
}
rq.url = url+"?op="+method;
rq.method = URLRequestMethod.POST;
rq.requestHeaders.push(new URLRequestHeader("Content-Type", "text/xml;charset=utf-8"));
rq.requestHeaders.push(new URLRequestHeader("SOAPAction", nns+"/"+method));
rq.data =tempXML;
addEventListener(Event.COMPLETE,comleteHandler);
load(rq);
}
function comleteHandler(evt:Event):void
{
var xml:XML = XML(data);
if(callBack!=null){
callBack.apply(null,new Array(xml.soap::Body.child(0).child(0)));
}
}
}
用法:
var kws:KitaWebServices = new KitaWebServices(url.text);
kws.call(webservices方法名:String,回调函数:Function,...args);
kws.call(webservices方法名:String,回调函数:Function,...args);
来源:https://www.cnblogs.com/sevenyuan/archive/2009/12/02/1615372.html