使用Flex访问ASP.net写的WebService

4 篇文章 0 订阅
使用Flex程序作为客户端访问用ASP.net写的Web服务的过程如下:

1。使用VS新建一个ASP.net Webservice Website,假设命名为Website9。编写一个HelloWorld方法,接受一个string类型的输入和一个string类型的输出。以下是这个WebService的源代码:
  1. using System;
  2. using System.Linq;
  3. using System.Web;
  4. using System.Web.Services;
  5. using System.Web.Services.Protocols;
  6. using System.Xml.Linq;
  7. [WebService(Namespace = "http://tempuri.org/")]
  8. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  9. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
  10. // [System.Web.Script.Services.ScriptService]
  11. public class Service : System.Web.Services.WebService
  12. {
  13.     public Service () {
  14.         //Uncomment the following line if using designed components 
  15.         //InitializeComponent(); 
  16.     }
  17.     [WebMethod]
  18.     public string HelloWorld(string input) {
  19.         return "Hello World,"+input;
  20.     }
  21.     
  22. }
2。为这个WebSite项目指定一个固定端口,假设为8888。运行WebSite9可以看到这个WebService的WSDL地址为: http://localhost:8888/WebSite9/Service.asmx?WSDL
打开这个地址找到以下信息,代表了HelloWorld函数接受的输入参数名为input,返回的参数名为HelloWorldResult。
  1. <s:element name="HelloWorld">
  2.   <s:complexType>
  3.      <s:sequence>
  4.         <s:element minOccurs="0" maxOccurs="1" name="input" type="s:string" /> 
  5.      </s:sequence>
  6.    </s:complexType>
  7. </s:element>
  8. <s:element name="HelloWorldResponse">
  9.   <s:complexType>
  10.      <s:sequence>
  11.         <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" /> 
  12.      </s:sequence>
  13.   </s:complexType>
  14. </s:element>


3。编写一个Flex程序以访问以上的WebService。以下通过两种方式:使用mxml和使用ActionScript。

方法一:使用mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12">
  3. <mx:Script>
  4.     <![CDATA[
  5.         import mx.rpc.events.ResultEvent;
  6.         import mx.rpc.events.FaultEvent;
  7.         
  8.         private var ws:WebService=new WebService();
  9.         
  10.         internal function sendMessage():void{
  11.             loader.HelloWorld.send();
  12.         }
  13.         
  14.         internal function completeHandler(evt:ResultEvent):void{
  15.             result_txt.text=evt.result.toString();
  16.         }
  17.         
  18.         internal function errorHandler(evt:FaultEvent):void{
  19.             result_txt.text=evt.fault.toString();
  20.         }
  21.     ]]>
  22. </mx:Script>
  23. <mx:WebService id="loader" wsdl="http://localhost:8888/WebSite9/Service.asmx?wsdl" result="completeHandler(event)" showBusyCursor="true" fault="errorHandler(event)">
  24.     <mx:operation name="HelloWorld">
  25.         <mx:request>
  26.         <input>         <!--输入参数的名字:input-->
  27.             {input_txt.text}
  28.         </input>
  29.         </mx:request>
  30.     </mx:operation>
  31.     
  32. </mx:WebService>
  33.     <mx:TextInput x="110" y="134" width="249" id="input_txt"/>
  34.     <mx:Button x="376" y="134" label="发送请求" id="send_btn" click="sendMessage()"/>
  35.     <mx:TextArea x="110" y="193" width="344" height="258" id="result_txt"/>
  36.     
  37. </mx:Application>
方法二:使用ActionScript
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12">
  3. <mx:Script>
  4.     <![CDATA[
  5.         import mx.rpc.events.ResultEvent;
  6.         import mx.rpc.events.FaultEvent;
  7.         import mx.rpc.soap.WebService;                //添加对WebService控件的引用
  8.         
  9.         private var ws:WebService=new WebService();
  10.         
  11.         internal function sendMessage():void{
  12.             loader.HelloWorld.send();
  13.             ws.HelloWorld.addEventListener("result",completeHandler);
  14.             ws.HelloWorld.addEventListener("fault",errorHandler);
  15.             ws.loadWSDL("http://localhost:8888/WebSite9/Service.asmx?WSDL");
  16.             var str:String=input_txt.text;
  17.             ws.HelloWorld(str);                         //直接调用远程函数
  18.         }
  19.         
  20.         internal function completeHandler(evt:ResultEvent):void{
  21.             result_txt.text=evt.result.toString();
  22.         }
  23.         
  24.         internal function errorHandler(evt:FaultEvent):void{
  25.             result_txt.text=evt.fault.toString();
  26.         }
  27.     ]]>

  28. </mx:Script>

  29.     
  30. </mx:WebService>
  31.     <mx:TextInput x="110" y="134" width="249" id="input_txt"/>
  32.     <mx:Button x="376" y="134" label="发送请求" id="send_btn" click="sendMessage()"/>
  33.     <mx:TextArea x="110" y="193" width="344" height="258" id="result_txt"/>
  34.     
  35. </mx:Application>

运行的结果如下:

             
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值