Flex的通信方式(五)——HTTPService_3

转载自:[url]http://hi.baidu.com/artgou/blog/item/0137ea993a4016006e068c3a.html[/url]

本文针对Flex的HTTPService的contentType的设置的两种通信数据格式进行介绍。


[HTTPService部分知识]
1、HTTPService的contentType属性,有"application/xml”和"application/x-www-form-urlencoded”两个可取值。在不进行设置的情况下,默认值为"application/x-www-form-urlencoded”。

2、HTTPService的method属性,有"POST"和"GET"两个可取值。在不进行设置的情况下,默认值为"GET"。


[b][color=red]下面用两个例子进行介绍说明:[/color][/b]


[例子_1]
HTTPService的contentType属性,取默认值"application/x-www-form-urlencoded”通信数据格式。

1、通讯的对象HTTPService的配置

<mx:HTTPService showBusyCursor="true"
id="getuser"
result="getuserproc();"
invoke="senduserproc(event);"
method="post"
url="http://localhost/Second.aspx"
fault="onFault(event);">
<mx:request>
<username>
{this.txtUserName.text}
</username>
<userpassword>
{this.txtUserPassWord.text}
</userpassword>
</mx:request>
</mx:HTTPService>


2、回传结果处理函数:

public function getuserproc():void
{
var returnValue:String=getuser.lastResult.Result.chu888;

if(returnValue=="ok")
{
Alert.show("您成功的登录了","提示信息",Alert.OK,this,null,null,Alert.YES);
}
else
{
Alert.show("您的登录失败了","提示信息",Alert.OK,this,null,null,Alert.YES);
}
}


3、参数说明:
url:指向提交地址
result\fault\invoke是3个事件,分别结果返回处理事件、错误返回处理事件、提交处理事件
method:http提交的方式
contentType默认:application/x-www-form-urlencoded
resultFormat默认:object
<mx:request>是提交的数据集合,可以参看flex的帮助。

4、发送的数据
准备向服务器请求提交,
那它触发后,发送数据格式:Object of name-value pairs。
比如:

body = (Object)#1
username = "s"
userpassword = "s"


5、服务器处理方法和返回的数据格式
服务器端的处理很简单:就是用Request.Params就能接收到;
比如:
Request.Params["username"],如上面的,接收值=s

返回给客户端的数据采用xml格式,直接Response.Write:

<Result>
<ErrorCode>{0}</ErrorCode>
<Description>{0}</Description>
</Result>



[例子_2]
HTTPService的contentType属性,取默认值"application/xml”通信数据格式。

1、通讯的对象HTTPService的配置

<mx:HTTPService id="xmlRequest"
showBusyCursor="true"
method="post"
contentType="application/xml"
url="http://218.200.200.176/Third.aspx"
resultFormat="xml"
result="xmlRequestproc();"
invoke="senduserproc(event);"
fault="onFault(event);">
<mx:request xmlns="">
<username>
{this.txtUserName.text}
</username>
<userpassword>
{this.txtUserPassWord.text}
</userpassword>
</mx:request>
</mx:HTTPService>


2、回传结果处理函数:

public function xmlRequestproc():void
{
this.txtResultData.text += this.xmlRequest.lastResult;
}


3、重点参数说明:
contentType:application/xml
resultFormat:xml

4、发送的数据
开始通讯,发送数据是xml文档,如下格式:
body = "<username>f</username><userpassword>f</userpassword>"

5、服务器处理方法和返回的数据格式
服务器端的处理,要使用Request.InputStream,并使用XmlDocument来解析
如下:

XmlDocument doc = new XmlDocument();

Stream str = Request.InputStream;
int len = (int)str.Length;
StringBuilder sb = new StringBuilder();

Byte[] strArr = new Byte[len];
int strRead = str.Read(strArr,0,len);

for(int counter=0; counter < len; counter++)
{
sb.Append((char)strArr[counter]);
}

if (sb.Length > 0)
{
doc.LoadXml("<Request>"+sb.ToString()+"</Request>");
XmlNode root = doc.FirstChild;
if (root.HasChildNodes)
{
for (int i=0; i<root.ChildNodes.Count; i++)
{
switch(i)
{
case 0:
username = (root.ChildNodes[i].InnerText);
file.WriteLine("U" + username);
break;
case 1:
password = (root.ChildNodes[i].InnerText);
file.WriteLine("P" + password);
break;
default:
break;
}
}
}
}


最后输出也是xml结构。


[总结]
contentType的设置,要根据实际的应用
服务器端处理是要根据contentType来处理
返回的数据最好是xml格式的数据,方便转换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值