下面是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/sayURLRequestHeader不支持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源文件下载:
as3_soap.rar
C# WebService源文件下载:
WebService.rar