HttpService定义:
在 MXML 文件中使用 <mx:HTTPService> 标签代表 HTTPService 对象。当调用 HTTPService 对象的 send() 方法时,
将发出对指定 URL 的 HTTP 请求,并且返回 HTTP 响应。可以选择向指定 URL 传递参数。如果没有使用基于服务器的代理服务,
则只能使用 HTTP GET 或 POST 方法。如果将 useProxy 属性设置为 true 并使用基于服务器的代理服务,则还可以使用
HTTP HEAD、OPTIONS、TRACE 和 DELETE 方法。
MXML 语法如下:
<mx:HTTPService
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."
fault="No default."
result="No default."
/>
常用属性:
id
method
resultFormat
url
useProxy
常用事件:
fault
result
数据加载请求例子
客户端:
通过标签来实现
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600"
creationComplete="application1_creationCompleteHandler(event)">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
/**
* 调用失败处理
*/
protected function http_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.toString());
}
/**
* 返回结果处理
*/
protected function http_resultHandler(event:ResultEvent):void
{
Alert.show(event.result.toString());
}
/**
* 初始化
*/
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
http.send();
}
]]>
</mx:Script>
<mx:HTTPService id="http" resultFormat="array" method="POST" url="http://localhost:6196/Web/Default.aspx"
useProxy="false" fault="http_faultHandler(event)" result="http_resultHandler(event)"/>
</mx:Application>
通过代码来实现
/**
* 调用失败处理
*/
protected function http_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.toString());
}
/**
* 返回结果处理
*/
protected function http_resultHandler(event:ResultEvent):void
{
Alert.show(event.result.toString());
}
/**
* 初始化
*/
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var service:mx.rpc.http.HTTPService = new mx.rpc.http.HTTPService();
service.url = "http://localhost:6196/Web/Default.aspx";
service.useProxy = false;
service.resultFormat="text";
service.addEventListener(ResultEvent.RESULT,http_resultHandler);
service.addEventListener(FaultEvent.FAULT,http_resultHandler);
service.send();
}
服务端:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Hello Flex HttpService");
}
参数传递方式共两种
客户端
第一种
/**
* 调用失败处理
*/
protected function http_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.toString());
}
/**
* 返回结果处理
*/
protected function http_resultHandler(event:ResultEvent):void
{
Alert.show(event.result.toString());
}
/**
* 初始化
*/
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var service:mx.rpc.http.HTTPService = new mx.rpc.http.HTTPService();
service.url = "http://localhost:6196/Web/Default.aspx?a=yang&b=xiao";
service.useProxy = false;
service.resultFormat="text";
service.addEventListener(ResultEvent.RESULT,http_resultHandler);
service.addEventListener(FaultEvent.FAULT,http_resultHandler);
service.send();
}
第二种
/**
* 调用失败处理
*/
protected function http_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.toString());
}
/**
* 返回结果处理
*/
protected function http_resultHandler(event:ResultEvent):void
{
Alert.show(event.result.toString());
}
/**
* 初始化
*/
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
var service:mx.rpc.http.HTTPService = new mx.rpc.http.HTTPService();
service.url = "http://localhost:6196/Web/Default.aspx";
service.useProxy = false;
service.resultFormat="text";
service.addEventListener(ResultEvent.RESULT,http_resultHandler);
service.addEventListener(FaultEvent.FAULT,http_resultHandler);
var param:URLVariables = new URLVariables();
param.a = "yang";
param.b = "xiao";
service.send(param);
}
服务端
public string A
{
get
{
object o = Request.QueryString["a"];
if (o == null)
{
return "";
}
return o.ToString();
}
}
public string B
{
get
{
object o = Request.QueryString["b"];
if (o == null)
{
return "";
}
return o.ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Hello Flex HttpService\r\n");
Response.Write(A + "\r\n");
Response.Write(B + "\r\n");
}
返回的数据类型