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

下面是as3访问Web Services的原理和过程,包括实例和源文件,已经经过了测试(http://www.roading.net/WebService/as3_soap.swf)...

前段时间写了 使用flash9(as3)连接webservice,结果发现这种以http post方法访问WebServices只能在测试环境下使用.然后就写了flash9/as3访问WebService的暂时替代方法,当然这是无奈之举,找不到合适的方法前先使用中转的方法来代替.

但是还是需要找到真正的解决方法,昨天在翻看flash8的mx\services包的时候,在包里面的SOAPCall和PendingCall类里面有整个的访问方法.

在SOAPCall类里面有request和response两个对象,分别是提交数据和返回数据.

下面是节选SOAPCall类的asyncInvoke方法的一部分,实现request的构造和数据发送(这里是流程,具体实现细节在PendingCall类里面):

//callback是PendingCall的实例.
callback.encode();

callback.callbackMethod = callbackMethod; // Callback method

// Populate parameters
callback.setupParams(args);

// prepare response object
var response = new XML();
response.ignoreWhite = true;
response.callback = callback;
response._startTimeMark = startTime;


callback.response = response;

// create the async response mechanism
response.onData = function(src)
{
}
// fire message
callback.request.sendAndLoad(this.endpointURI, response, "POST");
//-------------------------------------------------------------------------------------------

看到上面的代码,就会豁然开朗,就是使用soap协议,来提交和获取数据.
那么,我们就可以很简单的构成一个SOAP 请求.

我们看一下soap请求的格式(http://roading.net/WebService/test.asmx?op=say):
下面是一个 SOAP 请求和响应示例。所显示的占位符需要由实际值替换。

POST /WebService/test.asmx HTTP/1.1
Host: roading.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.roading.net/say"

<?xml version="1.0" encoding="utf-8"?>
<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/">
<soap:Body>
<say xmlns="http://www.roading.net/">
<str>string</str>
</say>
</soap:Body>
</soap:Envelope>

一个soap请求包括头部和数据.
soap请求头部包括:
POST /WebService/test.asmx HTTP/1.1
Host: roading.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.roading.net/say"
URLRequestHeader不支持post,host和Content-Length(ArgumentError: Error #2096: HTTP 请求标头 host 不能通过 ActionScript 设置。),同时也不必要,必须设置的是Content-Type和SOAPAction.
//
r.requestHeaders.push(new URLRequestHeader("Content-Type", "text/xml;charset=utf-8"));
r.requestHeaders.push(new URLRequestHeader("SOAPAction", "http://www.roading.net/say"));
//

soap请求数据为:
<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/">
 <say xmlns="http://www.roading.net/"> //调用方法.. 命名空间
  <str>hello</str> //参数
 </say>
</soap:Envelope>


整个的soap请求如上面所示...

就可以使用URLLoader和URLRequest类来发送和接收数据了.


下面是一个完整的调用WebServices的测试代码(不包括解析接收的数据):
//WebService网址(为测试写的例子) http://www.roading.net/WebService/test.asmx
import flash.net.*;
var soap:Namespace = new Namespace("http://schemas.xmlsoap.org/soap/envelope/");

var r:URLRequest = new URLRequest("http://www.roading.net/WebService/Test.asmx?op=say");
r.method = URLRequestMethod.POST;
r.requestHeaders.push(new URLRequestHeader("Content-Type", "text/xml;charset=utf-8"));
r.requestHeaders.push(new URLRequestHeader("SOAPAction", "http://www.roading.net/say"));


var rXML:XML =
    <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/">
     <soap:Body/>
    </soap:Envelope>
    ;
   
rXML.soap::Body.appendChild(
 <say xmlns="http://www.roading.net/"> //
  <str>hello</str> //
 </say>
);

r.data = rXML;

var l:URLLoader = new URLLoader();
l.dataFormat = URLLoaderDataFormat.TEXT;
l.load(r);

l.addEventListener("ioError" ,err);
l.addEventListener(Event.COMPLETE,xmlLoaded);
function xmlLoaded(d)
{
 trace(l.data);
 t.text = l.data;
}

function err(e)
{
 trace(e);
}



下载文件 flash源文件下载
下载文件 C# WebService源文件下载



评论: 6 | 查看次数: 8751
  • 1
jack [2008-02-20 11:26 AM]
我想用AS3做个天气预报,对soap协议又不大了解,有点看不懂
wander [2007-10-27 09:59 AM]
谢谢!
roading [2007-07-19 04:26 PM]
引用来自 frank 引用来自 frank
roading对webservice的探索真所谓一波三折了。赞一下研究结果.
顺便,谢谢Adobe告诉了我们"自己动手,丰衣足食"这个道理.


其实问题本身并不难,主要是开始没有仔细的对待...大意了...
frank [2007-07-18 09:28 AM]
roading对webservice的探索真所谓一波三折了。赞一下研究结果.
顺便,谢谢Adobe告诉了我们"自己动手,丰衣足食"这个道理.
roading [2007-07-16 12:36 AM]
引用来自 Richbox 引用来自 Richbox
Flash9/as3对Webservice的支持这么弱吗,怎么还不如以前

不是支持不好,网络访问更能在Flash9/as3里面应该说是加强了,但是在flash8的时候提供了比较多的类包,把以前功能给封装好了(比如Services包).由于adobe现在要靠flex来挣钱,所以把flash9里面的东西给减少,让大家用flex呢...
Richbox [2007-07-16 09:59 AM]
Flash9/as3对Webservice的支持这么弱吗,怎么还不如以前
  • 1
发表评论
昵 称:
密 码: 游客发言不需要密码.
验证码: =6+3
内 容:
  • 粗体
  • 斜体
  • 下划线
  • 插入引用
  • 表情符号
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 300 字 | UBB代码 关闭 | [img]标签 关闭