智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...

导航

as3访问WebService,SOAP协议

Posted on 2009-12-01 17:58  Bill Yuan  阅读(2566)  评论(0编辑  收藏  举报
代码
//功能实现:访问WebService,传入一张大图,以及需要的尺寸,由server转换成小图,返回图片流。
//实现要点:获取wsdl,解析XML(wsdl.data),获取需要的方法,以soap协议方式调用方法传递参数,并获取返回值(图片ByteArray),显示。

//代码:
//ServiceLoader.as

package com.meetexpo.webService {
    
import flash.net.URLLoader;
    
import flash.net.URLRequest;
    
import flash.net.URLLoaderDataFormat;
    
import flash.net.URLRequestHeader;
    
import flash.net.URLRequestMethod;
    
import flash.events.*;
    
    
public class ServiceLoader extends URLLoader {
        
private var _hostName:String;
        
private var _namespace:String;
        
private var _serviceName:String;
        
private var _parameters:Array;
        
        
public function ServiceLoader(hostName:String,nameSpace:String,serviceName:String,parameters:Array) {
            _hostName 
= hostName;
            _namespace 
= nameSpace;
            _serviceName
=serviceName;
            _parameters 
= parameters;
            var serviceRequest:URLRequest 
= generateURLRequest();
            
            
this.dataFormat = URLLoaderDataFormat.TEXT;
            
this.load(serviceRequest);
            
//this.addEventListener("ioError" ,err);
            
//this.addEventListener(Event.COMPLETE,xmlLoaded);
        }
        
private function generateURLRequest():URLRequest {
            var soap:Namespace 
= new Namespace("http://schemas.xmlsoap.org/soap/envelope/");
            var serviceRequest:URLRequest 
= new URLRequest(_hostName);
            serviceRequest.method 
= URLRequestMethod.POST;
            serviceRequest.requestHeaders.push(
new URLRequestHeader("Content-Type""text/xml;charset=utf-8"));
            serviceRequest.requestHeaders.push(
new URLRequestHeader("SOAPAction", _namespace+"/"+_serviceName));
            var rXML:XML 
=
            
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://logic.backend.imageservice.inshow.com" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" >
            
<soap:Body/>
            
</soap:Envelope>;
            
            
//var conditionStr:String = "<"+_serviceName+" xmlns=\"http://tempuri.org/\">";
            var conditionStr:String = "<" + _serviceName + " xmlns=\"" + _namespace + "\">";
            
            
for (var i:Number=0; i<_parameters.length; i++) {
            conditionStr
+="<"+_parameters[i][0]+">"+_parameters[i][1]+"</"+_parameters[i][0]+">";
            }
            
            conditionStr 
+= "</" + _serviceName + ">";
            
            var conditionXML:XML 
= new XML(conditionStr);
            rXML.soap::Body.appendChild(conditionXML);
            trace(rXML)
            serviceRequest.data 
= rXML;
            
return serviceRequest;
        }
      
        
public function xmlLoaded(e:Event):void{
                
//trace("--------------------")
                
//trace(e.eventInfo.method)
                
//trace(e.eventInfo.data)
                
//trace("--------------------")
                var listXML:XML = XML(this.data);
                trace(
">>>>") ;
            }
            
public function err(e:Event):void{
                trace(
">>>>") ;
            } 
    }
}

//Main.as

package com
{
    
import com.ServiceLoader;
    
import com.Base64;
    
import flash.display.Bitmap;
    
import flash.display.Loader;
    
import flash.display.Sprite;
    
import flash.events.Event;
    
import flash.display.BitmapData;
    
import flash.utils.ByteArray;
    
    
public class Main extends Sprite
    {
        
public function Main():void
        {
            var parameters:Array 
= new Array();
            parameters[
0= ["in0""http://www.bit-101.com/speaking/FITCAM2009.jpg"];
            parameters[
1= ["in1""100"];
            parameters[
2= ["in2""100"];
            var loader:ServiceLoader 
= new ServiceLoader("http://192.168.1.19:8080/image-service/services/ImageMagicService","http://logic.backend.imageservice.inshow.com","imgResize",parameters);
            
            loader.addEventListener(
"ioError" ,err);
            loader.addEventListener(Event.COMPLETE, xmlLoaded);
            
//loader.addEventListener(ProgressEvent.PROGRESS,singleHouseProgressHandler);

            function xmlLoaded(e:Event):
void{
                
//trace("--------------------")
                
//trace(e.eventInfo.method)
                
//trace(e.eventInfo.data)
                
//trace("--------------------")
                /*var loader:Loader = new Loader();
                var binary:ByteArray = evt.target.data as ByteArray;
                loader.loadBytes(binary);
                addChild(loader);
*/
                var listXML:XML 
= XML(loader.data);
                
//addChild(listXML)
                
//e.MyEventerGC(); 
                var tempLoader:Loader = new Loader();
                
//解码
                var decoded:ByteArray = Base64.decodeToByteArray(listXML.children().children().children());   
                tempLoader.loadBytes(decoded);
                addChild(tempLoader);
            }
            function err(e:Event):
void{
                trace(
">>>>") ;
                
//e.MyEventerGC(); 
            }
        }
    }
}