1.使用mxml标签定义WebService
a、定义webservice
<mx:WebService id="login_ws" wsdl="http://localhost/myprj/service/UserService?wsdl">
<mx:operation name="login" result="loginresult(event)" />
</mx:WebService>
b、 as中调用webservice进行登录身份的验证
private function userlogin():void
{
login_ws.login.send();
}
//登录处理函数
private function loginresult(event:ResultEvent):void
{
Alert.show("hhhh-"+event.result.toString());
}
2.在as中定义WebService
a、定义webservice
package com.util.service
{
import mx.controls.Alert;
import mx.managers.CursorManager;
import mx.rpc.AbstractOperation;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.soap.SOAPHeader;
import mx.rpc.soap.WebService;
public class MyWebService extends WebService
{
private var _callBackHandler:Function;
private var _faultHandler:Function;
private var _args:Array;
/**
* Construct
* @wsdl:webservice url
*/
public function MyWebService(wsdl:String)
{
super();
this.wsdl = wsdl;
this.loadWSDL();
CursorManager.setBusyCursor();
}
public function set callBackHandler(callBackHandler:Function):void
{
this._callBackHandler = callBackHandler;
}
public function get callBackHandler():Function
{
return this._callBackHandler;
}
public function set args(args:Array):void
{
this._args = args;
}
public function get args():Array
{
return this._args;
}
public function set faultHandler(faultHandler:Function):void
{
this._faultHandler = faultHandler;
}
public function get faultHandler():Function
{
return this._faultHandler;
}
public function initHeader(header:SOAPHeader):void
{
this.clearHeaders();
this.addHeader(header);
}
public function sendOperation(functionName:String,args:Array):void
{
//根据方法名动态调用WebService服务器的方法
var operation:AbstractOperation = this.getOperation(functionName);
//为调用的方法添加监听器,回调函数由外部动态传入
operation.addEventListener(ResultEvent.RESULT,this.callBackHandler);
//为调用的方法添加错误监听器,如果传入的错误处理方法为空,则调用默认的处理方法
if(this.faultHandler != null)
{
operation.addEventListener(FaultEvent.FAULT,this.faultHandler);
}
else
{
operation.addEventListener(FaultEvent.FAULT,defaultFaultHandler);
}
//为调用的方法传参数,参数类型为Array
operation.arguments = args;
//执行调用的方法
operation.send();
}
private function defaultFaultHandler(event:FaultEvent):void
{
CursorManager.removeBusyCursor();
}
}
}
b、应用webservice登录
var ar_arg:Array = new Array();
ar_arg.push(txtipt_username.text);
ar_arg.push(txtipt_password.text);
var mysrv:MyWebService = new MyWebService("http://localhost/myprj/service/UserService?wsdl");
mysrv.callBackHandler = loginHandler;
mysrv.sendOperation("loginManager",ar_arg);
// 登录处理函数
private function loginHandler(evt:ResultEvent):void
{
//通过evt.message.body.toString()可以看到服务端返回的信息,进而进行处理
Alert.show(evt.message.body.toString());
}