hanyeah 专注于AS

as3调用webservice方法

as3调用基于soap的webservice。

参考:http://www.cnblogs.com/sevenyuan/archive/2009/12/01/1614767.html

as3没有直接实现soap,但是,soap是基于http+xml的,使用的是http协议,发送的内容通过时xml格式的,as3支持http请求和xml,还可以设置http请求的头信息,因此,as3调用基于soap的webservice应该是不难的。

本站的一个webservice,可用于测试:http://hanyeah.com/study/aspnet/firstDemo/Service1.asmx

代码:

var xml: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>
					<HelloWorld xmlns="http://hanyeah.com/" />
				</soap:Body>
			</soap:Envelope>;
var uld:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest("http://hanyeah.com/study/aspnet/firstDemo/Service1.asmx");
req.method = URLRequestMethod.POST;
req.requestHeaders.push(new URLRequestHeader("Content-Type", "text/xml;charset=utf-8"));
req.requestHeaders.push(new URLRequestHeader("SOAPAction", "http://hanyeah.com/HelloWorld"));
req.data = "<?xml version='1.0' encoding='utf-8'?>" + xml.toXMLString();

uld.addEventListener(Event.COMPLETE, onLoaded);
uld.addEventListener(IOErrorEvent.IO_ERROR, onError);
uld.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
uld.load(req);


function onError(e:Event):void
{
	trace( e.toString());
}
function onLoaded(e:Event):void
{
	trace(uld.data);

}

如果返回securityError,可能需要配置host。


源码打包下载

2015年10月28日 | 发布:hanyeah | 分类:as3.0笔记 | 评论:0

发表留言: