转自:http://dev.yesky.com/265/3418765.shtml
本篇随笔主要是关于Flex与asp.net的WebService通讯,其中利用了SoapHeader。(如果你对SoapHeader不了解,可以参考WebService开发(一) 如何使用Soap头)。由此可见WebService在跨平台方面的优势。
ASP.NET的WebService代码
以下是引用片段: using System; [WebService(Namespace = "http://davidfan.cnblogs.com")]//名称空间应该和Flex中定义相同 } /**//// <summary> private string name; private string password; private DateTime exDate; |
Flex的mxml、as代码
mxml
以下是引用片段: <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontFamily="simsun" fontSize="12"> <mx:Script> <![CDATA[ import WebServiceUtility.Info; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.soap.SOAPHeader; private function SendRequest():void { var content:Info = new Info(); content.Name = UserName.text; content.Password = PassWord.text; content.ExDate = new Date(2010, 3, 10); //关键在QName的构造函数 //第一个参数要和.Net的WebService的名称空间相同 //第二个参数要和.Net的WebService的继承自SoapHeader的自定义类名称相同 var qname:QName = new QName("http://davidfan.cnblogs.com","ValidateSoapHeader"); var header:SOAPHeader = new SOAPHeader(qname,content); ws.clearHeaders(); ws.addHeader(header); ws.HelloWorld(); } private function OnResult(event:ResultEvent):void{ Result.text= event.result.toString(); } private function OnFault(event:FaultEvent):void{ Result.text= event.message.toString(); } ]]> </mx:Script> <mx:WebService id="ws" wsdl="http://localhost:9200/WebSite2/Service.asmx?WSDL" result="OnResult(event)" fault="OnFault(event)"/> <mx:TextInput id="UserName" x="98" y="94"/> <mx:TextInput id="PassWord" x="98" y="124"/> <mx:Label x="29" y="98" text="用户名:"/> <mx:Label x="29" y="128" text="密 码:"/> <mx:Button x="98" y="184" label="调用" click="SendRequest()"/> <mx:Label x="29" y="156" text="结 果:"/> <mx:TextInput id="Result" x="98" y="154"/> </mx:Application> |
as
以下是引用片段: package WebServiceUtility { public class Info{ //该类名称可以和.Net中的ValidateSoapHeader不同 //但字段要对应拼写相同 public var Name:String; public var Password:String; public var ExDate:Date; } } |