• flash9/flash cs3(as3)通过soap访问Web Services (.net)


      1flash9/flash cs3(as3)通过soap访问Web Services 
      2来源:Roading's blog 作者:roading 2007-07-17 【大 中小】 
      3
      4下面是as3访问Web Services的原理和过程,包括实例和源文件,已经经过了测试(http://www.roading.net/WebService/as3_soap.swf)
      5
      6前段时间写了 使用flash9(as3)连接webservice,结果发现这种以http post方法访问WebServices只能在测试环境下使用.然后就写了flash9/as3访问WebService的暂时替代方法,当然这是无奈之举,找不到合适的方法前先使用中转的方法来代替.
      7
      8但是还是需要找到真正的解决方法,昨天在翻看flash8的mx\services包的时候,在包里面的SOAPCall和PendingCall类里面有整个的访问方法.
      9
     10在SOAPCall类里面有request和response两个对象,分别是提交数据和返回数据.
     11
     12下面是节选SOAPCall类的asyncInvoke方法的一部分,实现request的构造和数据发送(这里是流程,具体实现细节在PendingCall类里面):
     13
     14
     15//callback是PendingCall的实例. 
     16callback.encode(); 
     17
     18callback.callbackMethod = callbackMethod; // Callback method 
     19
     20// Populate parameters 
     21callback.setupParams(args); 
     22
     23// prepare response object 
     24var response = new XML(); 
     25response.ignoreWhite = true
     26response.callback = callback; 
     27response._startTimeMark = startTime; 
     28
     29
     30callback.response = response; 
     31
     32// create the async response mechanism 
     33response.onData = function(src) 
     34
     35}
     
     36// fire message 
     37callback.request.sendAndLoad(this.endpointURI, response, "POST"); 
     38//------------------------------------------------------------------------------------------- 
     39
     40
     41看到上面的代码,就会豁然开朗,就是使用soap协议,来提交和获取数据.那么,我们就可以很简单的构成一个SOAP 请求.我们看一下soap请求的格式(http://roading.net/WebService/test.asmx?op=say):
     42下面是一个 SOAP 请求和响应示例。所显示的占位符需要由实际值替换。
     43
     44
     45POST /WebService/test.asmx HTTP/1.1 
     46Host: roading.net 
     47Content-Type: text/xml; charset=utf-8 
     48Content-Length: length 
     49SOAPAction: "http://www.roading.net/say" 
     50
     51<?xml version="1.0" encoding="utf-8"?> 
     52<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
     53<soap:Body> 
     54<say xmlns="http://www.roading.net/"> 
     55<str>string</str> 
     56</say> 
     57</soap:Body> 
     58</soap:Envelope> 
     59
     60
     61一个soap请求包括头部和数据.
     62soap请求头部包括:
     63
     64
     65POST /WebService/test.asmx HTTP/1.1 
     66Host: roading.net 
     67Content-Type: text/xml; charset=utf-8 
     68Content-Length: length 
     69SOAPAction: http://www.roading.net/say 
     70
     71
     72URLRequestHeader不支持post,host和Content-Length(ArgumentError: Error #2096: HTTP 请求标头 host 不能通过 ActionScript 设置。),同时也不必要,必须设置的是Content-Type和SOAPAction.
     73
     74
     75// 
     76r.requestHeaders.push(new URLRequestHeader("Content-Type""text/xml;charset=utf-8")); 
     77r.requestHeaders.push(new URLRequestHeader("SOAPAction""http://www.roading.net/say")); 
     78// 
     79
     80
     81soap请求数据为:
     82
     83
     84<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
     85 <say xmlns="http://www.roading.net/"> //调用方法.. 命名空间 
     86  <str>hello</str> //参数 
     87 </say> 
     88</soap:Envelope> 
     89
     90
     91整个的soap请求如上面所示就可以使用URLLoader和URLRequest类来发送和接收数据了.下面是一个完整的调用WebServices的测试代码(不包括解析接收的数据):
     92
     93
     94//WebService网址(为测试写的例子) http://www.roading.net/WebService/test.asmx 
     95import flash.net.*
     96var soap:Namespace = new Namespace("http://schemas.xmlsoap.org/soap/envelope/"); 
     97
     98var r:URLRequest = new URLRequest("http://www.roading.net/WebService/Test.asmx?op=say"); 
     99r.method = URLRequestMethod.POST; 
    100r.requestHeaders.push(new URLRequestHeader("Content-Type""text/xml;charset=utf-8")); 
    101r.requestHeaders.push(new URLRequestHeader("SOAPAction""http://www.roading.net/say")); 
    102
    103
    104var rXML:XML =  
    105    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    106     <soap:Body/> 
    107    </soap:Envelope> 
    108    ; 
    109    
    110rXML.soap::Body.appendChild( 
    111 <say xmlns="http://www.roading.net/"> // 
    112  <str>hello</str> // 
    113 </say> 
    114); 
    115
    116r.data = rXML; 
    117
    118var l:URLLoader = new URLLoader(); 
    119l.dataFormat = URLLoaderDataFormat.TEXT; 
    120l.load(r); 
    121
    122l.addEventListener("ioError" ,err); 
    123l.addEventListener(Event.COMPLETE,xmlLoaded); 
    124function xmlLoaded(d) 
    125
    126 trace(l.data); 
    127 t.text = l.data; 
    128}
     
    129
    130function err(e) 
    131
    132 trace(e); 
    133}
     
    134
  • 相关阅读:
    封装aixos拦截器
    vue路由传参的三种基本方式
    vue里的路由钩子
    箭头函数特点
    webstorm激活码
    vue-cli2使用less
    vue-cli2使用rem适配
    XfZGkvBaeh
    python解析excel中图片+提取图片
    python解析谷歌在线表格链接,转化为数组形式,图片转化为链接
  • 原文地址:https://www.cnblogs.com/wubiyu/p/1239584.html
Copyright © 2020-2023  润新知