加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用Flex访问ASP.net写的WebService .

发布时间:2020-12-15 04:11:19 所属栏目:百科 来源:网络整理
导读:使用Flex程序作为客户端访问用ASP.net写的Web服务的过程如下: 1。使用VS新建一个ASP.net Webservice Website,假设命名为Website9。编写一个HelloWorld方法,接受一个string类型的输入和一个string类型的输出。以下是这个WebService的源代码: using ?Syste
使用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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读