首页  韩国资源  酷站加油  我的展厅  设计名站  古典元素  推荐下载  设计欣赏  每周专访  招募精英  人才专区  网页教程  平面设计  编程开发  设计竞赛
当前位置:首页 > 网页教程 > Flash教程 > 正文
Google
flash9/flash cs3(as3)通过soap访问Web Services
来源:Roading's blog 作者:roading 2007年07月17日 16:54 网友评论:0条 点击:
下面是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源文件下载:as3_soap.rar

C# WebService源文件下载:WebService.rar
上一篇:Flash游戏制作基础:跟随鼠标的曲线   下一篇:拖动,缓动,加投影效果(提供源码下载)
收藏此页】【打印】【关闭
 相关文章  我要点评

免责声明:本站刊载此文不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。对本文有任何异议,请联络:68design#163.com
转载要求:作者及来源信息必需保留。转载之图片、文件,链接请不要盗链到本站,且不准打上各自站点的水印。



关于我们 | 在线反馈 | 广告报价 | 友情链接 | 联系我们 | 免责声明 | 在线投稿 | 网站地图
Copyright © 2003-2007 68design.net, All Rights Reserve 【找网页设计师,当然上网页设计师联盟】