Flex的HTTPService 学习001

[说明]:

本文针对Flex的HTTPService的contentType的设置以及服务段对此的不同接收和处理方式。

本文服务器端程序:Net

[HTTPService部分知识]

在HTTPService的contentType中,有两种类型“application/xml”和“application/x-www-form-urlencoded”(默认的)。

无论哪种类型,都可以使用“Post”方式发送数据。

"Post"该对象的method属性的一个值,还包括"Get",这个是默认值

[例子]

先看下简单例子:
通讯的对象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>
<mx:Script>

回传结果处理函数:
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);
 }
}

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

准备向服务器请求提交,
那它触发后,发送数据格式:Object of name-value pairs。
比如: 
body = (Object)#1
    username = "s"
    userpassword = "s"

服务器端的处理很简单:就是用Request.Params就能接收到;
比如:
Request.Params["username"],如上面的,接收值=s

返回给客户端的数据采用xml格式,直接Response.Write:
<Result>
 <ErrorCode>{0}</ErrorCode>
 <Description>{0}</Description>
</Result>

__________________________________________________________________________
另一个例子:
通讯的对象设置如下
<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>   

回传处理函数:
public function xmlRequestproc():void

 this.txtResultData.text += this.xmlRequest.lastResult; 
}

重点说明这两个参数:
contentType:application/xml
resultFormat:xml

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

服务器端的处理,要使用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格式的数据,方便转换



原文地址:https://www.cnblogs.com/GoGoagg/p/1294176.html