hanyeah 专注于AS

as3非soap方式调用webservice方法

之前介绍了as3通过soap方式调用webservice的方法,现在介绍直接用get,post方式调用。

测试使用的本站的一个webservicehttp://hanyeah.com/study/aspnet/firstDemo/Service1.asmx,建议打开看一看,用微软的asp.net技术生成的,微软的东西做的还是很详细的。

as3测试代码:

package  {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	
	
	public class Main extends MovieClip {
		
		
		public function Main() {
			// constructor code
			var uld:URLLoader = new URLLoader();
			var req:URLRequest = new URLRequest("http://hanyeah.com/study/aspnet/firstDemo/Service1.asmx/HelloWorld? HTTP/1.1");
			req.method = URLRequestMethod.GET;
			req.requestHeaders.push(new URLRequestHeader("Content-Type", "text/xml;charset=utf-8"));
			//req.requestHeaders.push(new URLRequestHeader("Content-Length", "100"));
			
			uld.addEventListener(Event.COMPLETE, onLoaded);
			uld.addEventListener(IOErrorEvent.IO_ERROR, onError);
			uld.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
			
			var t:Number = new Date().getTime();
			btn.addEventListener(MouseEvent.CLICK, click);
			function click(e:Event):void {
				t = new Date().getTime();
				uld.load(req);
			}
			
			click(null);
			function onError(e:Event):void {
				tf.text = e.toString()+"\n用时:"+(new Date().getTime()-t)+"ms";
			}
			function onLoaded(e:Event):void {
				trace(uld.data);
				tf.text = uld.data.toString()+"\n用时:"+(new Date().getTime()-t)+"ms";
				
			}
		}
	}
	
}

其实就是用as3直接访问http://hanyeah.com/study/aspnet/firstDemo/Service1.asmx/HelloWorld? HTTP/1.1这个地址。

Content-Type可以加也可以不加,可能默认就是utf-8吧(不了解)。

Content-Length,as3是不支持的,无法通过as3添加,百度了一下“Content-Length”,没看懂,但是上面这个例子不加也没问题。


源码打包下载

2016年1月14日 | 发布:hanyeah | 分类:as3.0笔记 | 评论:0

发表留言: