写了一个 http wrap和测试的例子:
//
// My System
// Copyright 2008-2009 My Systems Incorporated
// All Rights Reserved.
//
// NOTICE: My System permits you to use, modify, and distribute this file
// Langversion ActionScript 3.0
// playerversion Flash 9.0
// DATE: 2009-03-24 keren
// QQ: 994251681
// MSN: keren1515@hotmail.com
//
/**
<mx:HTTPService
【Properties】
concurrency="multiple|single|last"
contentType="application/x-www-form-urlencoded|application/xml"
destination="DefaultHTTP"
id="No default."
method="GET|POST|HEAD|OPTIONS|PUT|TRACE|DELETE"
resultFormat="object|array|xml|e4x|flashvars|text"
showBusyCursor="false|true"
makeObjectsBindable="false|true"
url="No default."
useProxy="false|true"
xmlEncode="No default."
xmlDecode="No default."
【Events】
fault="No default."
result="No default."
/>
*/
package org.app.utils.http
{
import com.hurlant.eval.ast.Void;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.InvokeEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
public class MyHttpServiceWrapper extends EventDispatcher
{
//----常量定义
public static const METHOD_GET:String = "GET";//默认
public static const METHOD_POST:String = "POST";
public static const METHOD_PUT:String = "PUT";
public static const METHOD_HEAD:String = "HEAD";
public static const METHOD_OPTIONS:String = "OPTIONS";
public static const METHOD_TRACE:String = "TRACE";
public static const METHOD_DELETE:String = "DELETE";
public static const CONCURRENCY_MUL:String = "multiple";//默认
public static const CONCURRENCY_SGL:String = "single";
public static const CONCURRENCY_LST:String = "last";
public static const CONTENT_TYPE_FORM:String = HTTPService.CONTENT_TYPE_FORM;//"application/x-www-form-urlencoded";//默认
public static const CONTENT_TYPE_XML:String = HTTPService.CONTENT_TYPE_XML;//"application/xml";
public static const RESULT_FORMAT_OBJ:String = HTTPService.RESULT_FORMAT_OBJECT;//"object";
public static const RESULT_FORMAT_ARR:String = HTTPService.RESULT_FORMAT_ARRAY;//"array";
public static const RESULT_FORMAT_XML:String = HTTPService.RESULT_FORMAT_XML;//"xml";//默认
public static const RESULT_FORMAT_E4X:String = HTTPService.RESULT_FORMAT_E4X;//"e4x";
public static const RESULT_FORMAT_FLV:String = HTTPService.RESULT_FORMAT_FLASHVARS;//"flashvars";
public static const RESULT_FORMAT_TXT:String = HTTPService.RESULT_FORMAT_TEXT;//"text";
public static const EVENT_RESULT:String = "result";//正确
public static const EVENT_FAULT:String = "fault";//失败
public static const EVENT_INVOKE:String = "invoke";//请求
//----成员定义
//http请求的信息 url method param 成功处理方法 错误处理方法 结果类型 结果集合
private var httpService:HTTPService = null;
private var url:String = null;
private var param:Object = null;
private var method:String = METHOD_GET;
private var contentType:String = CONTENT_TYPE_FORM;
private var resultFormat:String = RESULT_FORMAT_XML;
private var useProxy:Boolean = false;
private var showBusyCursor:Boolean = true;
private var result:Object = null;//处理结果[不管是错误还是请求正确的]
public function MyHttpServiceWrapper(target:IEventDispatcher=null)
{
super(target);
}
/**
* 该方法必须在 send()之前被调用
*
*/
public function prepare():void{
try{
this.httpService = new HTTPService();
this.httpService.url = this.url;
this.httpService.method = this.method;
this.httpService.contentType = this.contentType;
this.httpService.resultFormat = this.resultFormat;
//busyCursor/???/
this.httpService.addEventListener(ResultEvent.RESULT,resultHandler);
this.httpService.addEventListener(FaultEvent.FAULT,faultHandler);
this.httpService.addEventListener(InvokeEvent.INVOKE,invokeHandler);
}catch(e:Error){
throw e;
}
}
/**
* 与http后台的请求处理方法
*
*/
public function send(p:Object=null):void{
if(p != null) {this.param = p;}
this.httpService.request = this.param;
this.httpService.send(this.param);
}
private function resultHandler(e:ResultEvent):void{
//trace("--in result handler()"+e.target);
//trace("--in result handler()"+e.result);
this.result = e.result;
this.dispatchEvent(new Event(EVENT_RESULT));//自定义事件
}
private function faultHandler(e:FaultEvent):void{
//trace("--in fault handler()"+e);
//trace("--in fault handler()"+e.target.toLocaleString());
this.result = e;
this.dispatchEvent(new Event(EVENT_FAULT));//自定义事件
}
private function invokeHandler(e:InvokeEvent):void{
//trace("--in invoke handler()");
this.result = e;
this.dispatchEvent(new Event(EVENT_INVOKE));//自定义事件
}
public function setUrl(url:String):void{
this.url = url;
}
public function setParam(p:Object):void{
this.param = p;
}
public function seMethod(m:String):void{
this.method = m;
}
public function setContentType(cType:String):void{
this.contentType = cType;
}
public function setResultFormat(rsf:String):void{
this.resultFormat = rsf;
}
public function setUseProxy(uProxy:Boolean):void{
this.useProxy = uProxy;
}
public function setShowBusyCursor(sbc:Boolean):void{
this.showBusyCursor = sbc;
}
public function getResult():Object{
return this.result;
}
}//end class
}//end package
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12">
<mx:Script>
<![CDATA[
import org.app.utils.http.MyHttpServiceWrapper;
import org.app.utils.common.*;
import com.blitzagency.xray.logger.XrayLog;
private var log:XrayLog = new XrayLog();
private function doReq():void{
try{
trace("---开始。..");
var httpHandler:MyHttpServiceWrapper = new MyHttpServiceWrapper();
//初始化请求参数
httpHandler.setUrl(my_url.text);
httpHandler.setResultFormat(MyHttpServiceWrapper.RESULT_FORMAT_TXT);
trace("---开始。..111");
httpHandler.addEventListener(MyHttpServiceWrapper.EVENT_RESULT,handlerResult);
httpHandler.addEventListener(MyHttpServiceWrapper.EVENT_FAULT,falutHandler);
//准备
httpHandler.prepare();
//请求动作
httpHandler.send();
}catch(e:Error){
trace(e.message);
trace(e);
}
}
private function handlerResult(e:Event):void{
trace("--准备获取结果。"+e.target);
var o:Object = (e.target as MyHttpServiceWrapper).getResult();
trace(o);
rs.text = o.toString();
}
private function falutHandler(e:Event):void{
trace("--遇到错误。");
var o:Object = (e.target as MyHttpServiceWrapper).getResult();
trace(o);
rs.text = o.toString();
}
]]>
</mx:Script>
<mx:TextInput id="my_url" x="26" y="22" width="480" text="http://localhost:8080/test.jsp"/>
<mx:Button x="514" y="22" label="通讯" click="doReq();"/>
<mx:TextArea id="rs" x="26" y="52" width="480" height="191"/>
</mx:Application>