使用Flex程序作为客户端访问用ASP.net写的Web服务的过程如下:
1。使用VS新建一个ASP.net Webservice Website,假设命名为Website9。编写一个HelloWorld方法,接受一个string类型的输入和一个string类型的输出。以下是这个WebService的源代码:
2。为这个WebSite项目指定一个固定端口,假设为8888。运行WebSite9可以看到这个WebService的WSDL地址为: http://localhost:8888/WebSite9/Service.asmx?WSDL
打开这个地址找到以下信息,代表了HelloWorld函数接受的输入参数名为input,返回的参数名为HelloWorldResult。
3。编写一个Flex程序以访问以上的WebService。以下通过两种方式:使用mxml和使用ActionScript。
方法一:使用mxml
方法二:使用ActionScript
1。使用VS新建一个ASP.net Webservice Website,假设命名为Website9。编写一个HelloWorld方法,接受一个string类型的输入和一个string类型的输出。以下是这个WebService的源代码:
- using System;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.Xml.Linq;
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
- // [System.Web.Script.Services.ScriptService]
- public class Service : System.Web.Services.WebService
- {
- public Service () {
- //Uncomment the following line if using designed components
- //InitializeComponent();
- }
- [WebMethod]
- public string HelloWorld(string input) {
- return "Hello World,"+input;
- }
- }
打开这个地址找到以下信息,代表了HelloWorld函数接受的输入参数名为input,返回的参数名为HelloWorldResult。
- <s:element name="HelloWorld">
- <s:complexType>
- <s:sequence>
- <s:element minOccurs="0" maxOccurs="1" name="input" type="s:string" />
- </s:sequence>
- </s:complexType>
- </s:element>
- <s:element name="HelloWorldResponse">
- <s:complexType>
- <s:sequence>
- <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
- </s:sequence>
- </s:complexType>
- </s:element>
3。编写一个Flex程序以访问以上的WebService。以下通过两种方式:使用mxml和使用ActionScript。
方法一:使用mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12">
- <mx:Script>
- <![CDATA[
- import mx.rpc.events.ResultEvent;
- import mx.rpc.events.FaultEvent;
- private var ws:WebService=new WebService();
- internal function sendMessage():void{
- loader.HelloWorld.send();
- }
- internal function completeHandler(evt:ResultEvent):void{
- result_txt.text=evt.result.toString();
- }
- internal function errorHandler(evt:FaultEvent):void{
- result_txt.text=evt.fault.toString();
- }
- ]]>
- </mx:Script>
- <mx:WebService id="loader" wsdl="http://localhost:8888/WebSite9/Service.asmx?wsdl" result="completeHandler(event)" showBusyCursor="true" fault="errorHandler(event)">
- <mx:operation name="HelloWorld">
- <mx:request>
- <input> <!--输入参数的名字:input-->
- {input_txt.text}
- </input>
- </mx:request>
- </mx:operation>
- </mx:WebService>
- <mx:TextInput x="110" y="134" width="249" id="input_txt"/>
- <mx:Button x="376" y="134" label="发送请求" id="send_btn" click="sendMessage()"/>
- <mx:TextArea x="110" y="193" width="344" height="258" id="result_txt"/>
- </mx:Application>
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12">
- <mx:Script>
- <![CDATA[
- import mx.rpc.events.ResultEvent;
- import mx.rpc.events.FaultEvent;
- import mx.rpc.soap.WebService; //添加对WebService控件的引用
- private var ws:WebService=new WebService();
- internal function sendMessage():void{
- loader.HelloWorld.send();
- ws.HelloWorld.addEventListener("result",completeHandler);
- ws.HelloWorld.addEventListener("fault",errorHandler);
- ws.loadWSDL("http://localhost:8888/WebSite9/Service.asmx?WSDL");
- var str:String=input_txt.text;
- ws.HelloWorld(str); //直接调用远程函数
- }
- internal function completeHandler(evt:ResultEvent):void{
- result_txt.text=evt.result.toString();
- }
- internal function errorHandler(evt:FaultEvent):void{
- result_txt.text=evt.fault.toString();
- }
- ]]>
- </mx:Script>
- </mx:WebService>
- <mx:TextInput x="110" y="134" width="249" id="input_txt"/>
- <mx:Button x="376" y="134" label="发送请求" id="send_btn" click="sendMessage()"/>
- <mx:TextArea x="110" y="193" width="344" height="258" id="result_txt"/>
- </mx:Application>