AS3操作WebService
Posted in ActionScript on 24. 二, 2010
以在线天气预报为例
地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
方法:getWeatherbyCityName
一、直接操作
- GET
- import flash.net.URLLoader;
- import flash.net.URLRequest;
- var loader:URLLoader = new URLLoader(new URLRequest("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=上海"));
- loader.addEventListener(Event.COMPLETE, completeHandler);
- function completeHandler(ev:Event):void{
- trace(loader.data);
- }
- POST
- import flash.net.URLLoader;
- import flash.net.URLRequest;
- import flash.net.URLVariables;
- var vars:URLVariables = new URLVariables();
- vars.theCityName = "上海";
- var request:URLRequest = new URLRequest("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName");
- request.method = "post";
- request.data = vars;
- var loader:URLLoader = new URLLoader(request);
- loader.addEventListener(Event.COMPLETE, completeHandler);
- function completeHandler(ev:Event):void{
- trace(loader.data);
- }
- 常规访问(参考网上)
此方法中..我们使用常规的方法来访问WS,所有的WS都适用..
PS:暂时只有 http://www.webxml.com.cn/ 的WS做测试..(不懂后台..所以无法自己写WS测试..),如各位发现问题..可回复指出..首先..我们使用FLEX中的WS类写一个调用上述WS的程序..然后通过程序截获数据..
首先Head中信息为..
(Request-Line) POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Accept */*
Accept-Encoding gzip, deflate
Accept-Language zh-CN
Cache-Control no-cache
Connection Keep-Alive
Content-Length 375
Content-Type text/xml; charset=utf-8
Cookie WebXmlCookies=WeatherProvince=31124&WeatherCity=2419; ASP.NET_SessionId=qvrl3z45fqbngw55mdjysp3m; __utma=89798685.2765186755903630000.1237825717.1237861008.1237903668.4; __utmz=89798685.1237825717.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmb=89798685.1.10.1237903668; __utmc=89798685
Host www.webxml.com.cn
SOAPAction “http://WebXml.com.cn/getMobileCodeInfo”
User-Agent Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; QQDownload 1.7; TencentTraveler 4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)
x-flash-version 10,0,22,87
POST内容为13800138000 分析其中POST内容..发现为一个XML格式字符串请求..
格式为
其中
getMobileCodeInfo为方法名
mobileCode为参数
13800138000为mobileCode的值http://WebXml.com.cn/为该ws的Namespace
tns为任意名字..这里为tns(targetNamespace简写?)
知道格式后..我们就可以开始在AS3里拼接这些数据..然后访问WS
- package {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.net.URLLoader;
- import flash.net.URLRequest;
- import flash.net.URLRequestHeader;
- import flash.net.URLRequestMethod;
- public class WSExample3 extends Sprite
- {
- public function WSExample3()
- {
- init();
- }
- private function init():void
- {
- //拼接XML
- var s:XML = <SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <SOAP:Body>
- <tns:getMobileCodeInfo xmlns:tns="http://WebXml.com.cn/">
- <tns:mobileCode>13800138000</tns:mobileCode>
- <tns:userID></tns:userID>
- </tns:getMobileCodeInfo>
- </SOAP:Body>
- </SOAP:Envelope>
- //注意tns,getMobileCodeInfo,mobileCode,userID
- var request:URLRequest = new URLRequest("http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx")
- request.method = URLRequestMethod.POST;
- //添加requestHeaders,经测试和网上资料,只添加Content-Type和SOAPAction即可
- //Content-Type发送内容的格式..
- //SOAPAction由该ws的Namespace+方法组成..
- var headers:Array = [];
- headers.push(new URLRequestHeader("Content-Type", "text/xml; charset=utf-8"));
- headers.push(new URLRequestHeader("SOAPAction", "http://WebXml.com.cn/getMobileCodeInfo"));
- request.requestHeaders = headers;
- //指定请求内容
- request.data = s.toXMLString();
- var loader:URLLoader = new URLLoader();
- loader.addEventListener("complete", complete);
- loader.load(request)
- }
- private function complete(e:Event):void
- {
- trace(XML(e.target.data))
- //输出:
- //<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- // <soap:Body>
- // <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
- // <getMobileCodeInfoResult>13800138000:北京 北京 北京移动全球通卡</getMobileCodeInfoResult>
- // </getMobileCodeInfoResponse>
- // </soap:Body>
- //</soap:Envelope>
- //其中getMobileCodeInfoResult为我们需要的结果
- }
- }
- }
二、使用第三方类库
- Roading的WebService类
- import net.roading.net.WebService;
- //
- import flash.events.EventDispatcher;
- //
- var ws:WebService = new WebService("http://www.roading.net/WebService/test.asmx");
- //var ws:WebService = new WebService("http://www.wapit.cn/WebServices/Weather.asmx");
- //var ws:WebService = new WebService("http://webservice.k-zone.cn/URLToHtmlService/URLToHtmlService.asmx");
- ws.onLoad = function(e)
- {
- trace(this.getWsInfo());
- }
- ws.onError = function(e)
- {
- trace(e.toString());
- }
- //
- var sr:EventDispatcher = ws.call("getAge",'122',123);
- sr.addEventListener("complete", completeHandler);
- sr.addEventListener("error", errorHandler);
- function errorHandler(e)
- {
- trace('error');
- for(var i in e.info)
- trace(i,"=======",e.info[i]);
- }
- function completeHandler(e)
- {
- trace("-------------------------");
- trace("getAge result:");
- trace(e.xml);
- trace(e.data);
- }
- //
- var sr1:EventDispatcher = ws.call("getTime");
- sr1.addEventListener("complete", sr1completeHandler);
- function sr1completeHandler(e)
- {
- trace("-------------------------");
- trace("getTime result:");
- trace(e.xml);
- trace(e.data);
- }
- //
- var sr2:EventDispatcher = ws.call("say","i am roading","aha");
- sr2.addEventListener("complete", sr2completeHandler);
- function sr2completeHandler(e)
- {
- trace("-------------------------");
- trace("say result:");
- trace(e.xml);
- trace(e.data);
- }
- SnowMan的WebServices类
- import com.roading.net.WebServices;
- function analyseCompleteHandler(e){
- trace("解析完成: ", e.eventInfo.target, "所有方法如下: ");
- for(var i in e.eventInfo.methods)
- trace("方法名:" + i + " 参数:(" + e.eventInfo.methods[i] + ")");
- //解析完成后,可以调用方法了
- e.target.load("GetUserList", "jinguoxing");
- }
- function analyseErrorHandler(e){
- trace("解析出错: ", e.eventInfo.target, e.eventInfo.info);
- }
- function wsCallCompleteHandler(e){
- trace("调用完成: ", e.eventInfo.target, e.eventInfo.methodName);
- //e.eventInfo.data为返回的数据,可将根据e.eventInfo.methodName(调用的方法名)进一步分析数据.
- trace("返回的数据是: ", e.eventInfo.data);
- txt.text = e.eventInfo.data;
- //trace("返回的数据是: ", new XML(e.eventInfo.data));
- }
- function wsCallErrorHandler(e){
- trace("调用方法出错: ", e.eventInfo.target, e.eventInfo.info);
- }
- //两个步骤,首先解析WebServie,然后才可以调用WebServie方法
- var ws = new WebServices("http://192.168.1.218:156/list.asmx");
- ws.addEventListener("wsAnalyseComplete", analyseCompleteHandler);
- ws.addEventListener("wsAnalyseError", analyseErrorHandler);
- ws.addEventListener("wsCallComplete", wsCallCompleteHandler);
- ws.addEventListener("wsCallError", wsCallErrorHandler);
- 其他的MyWebService类
- import com.roading.net.MyWebService;
- var ws=new MyWebService("http://192.168.1.200:156/list.asmx");
- ws.load("GetUserList","ency=jinguoxing");
- ws.addEventListener("callComplete", wsCompleteHandler);
- ws.addEventListener("callError", wsIOErrorHandler);
- function wsIOErrorHandler(e):void {
- trace(e.eventInfo.method);
- e.MyEventerGC();
- }
- function wsCompleteHandler(e):void {
- //trace("--------------------")
- //trace(e.eventInfo.method)
- //trace(e.eventInfo.data)
- //trace("--------------------")
- //var listXML:XML=XML(e.eventInfo.data);
- trace(e.eventInfo.data);
- txt.text = e.eventInfo.data;
- e.MyEventerGC();
- }
来源:https://www.cnblogs.com/shinings/archive/2010/07/13/1776809.html