在Flex中使用HTTPService传递参数

先摘录HTTPService的adobe关于MXML的官方内容如下:

在 MXML 文件中使用 <mx:HTTPService> 标签代表 HTTPService 对象。当调用 HTTPService 对象的 send() 方法时,将发出对指定 URL 的 HTTP 请求,并且返回 HTTP 响应。可以选择向指定 URL 传递参数。如果没有使用基于服务器的代理服务,则只能使用 HTTP GET 或 POST 方法。如果将 useProxy 属性设置为 true 并使用基于服务器的代理服务,则还可以使用 HTTP HEAD、OPTIONS、TRACE 和 DELETE 方法。

注意:由于软件限制,当使用 GET 时 HTTPService 不生成用户界面友好的错误消息。

MXML 语法

The <mx:HTTPService> tag accepts the following tag attributes:

 <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."
 />

 HTTPServiceExample.mxml

<?xml version="1.0" encoding="utf-8"?>
<!-- Simple example to demonstrate the HTTPService tag. -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
     xmlns:s="library://ns.adobe.com/flex/spark"
     xmlns:mx="library://ns.adobe.com/flex/mx"
    creationComplete="feedRequest.send();">
       
        <!-- The url property specifies the location of the requested file,
        in this case the RSS 2.0 feed of Matt Chotin's blog.
        As of this writing, the URL was still valid, but you should
        check to make sure it hasn't changed.
        You should use the latest RSS 2.0 URL listed on the right side of
        the blog at http://www.adobe.com/go/mchotinblog. -->
       
   
    <fx:Declarations>
        <mx:HTTPService
            id="feedRequest"
            url="http://weblogs.macromedia.com/mchotin/index.xml"
            useProxy="false" />
    </fx:Declarations>
       
    <mx:Panel title="HTTPService Example" height="75%" width="75%"
        paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">
           
        <mx:DataGrid id="dgPosts" height="50%" width="75%"
            dataProvider="{feedRequest.lastResult.rss.channel.item}">
            <mx:columns>
                <mx:DataGridColumn headerText="Posts" dataField="title"/>
                <mx:DataGridColumn headerText="Date" dataField="pubDate"/>
            </mx:columns>
        </mx:DataGrid>
           
        <mx:TextArea height="50%" width="75%" htmlText="{dgPosts.selectedItem.description}"/>
    </mx:Panel>   
</s:Application>

源文档 <http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/mx/rpc/http/mxml/HTTPService.html>

按照MXML的语法传参数时可以使用标记,需要放在fx:Declarations标签里,例子如下:

<!-- 其余属性的设置参见MXML 语法 -->

<s:HTTPService id="service" url="[http://地址|https://地址]" method="POST" useProxy="false"

            resultFormat="xml" fault="[失败处理方法,记得把事件传过去]"

            result="[结果处理方法,记得把事件传过去]">

<s:request >

<!--参数名称作标签,中间填充参数值-->

<account>[account]</account>

<password>[password]</password>

</s:request>

</s:HTTPService>

也可以在ActionScript里使用对象的方式,例子如下:

<!-- 其余属性的设置参见MXML 语法 -->

var service:mx.rpc.http.HTTPService = new mx.rpc.http.HTTPService();

service.url = "[http://地址|https://地址]";

service.useProxy = false;

service.resultFormat="xml";

service.addEventListener(ResultEvent.RESULT,[结果处理方法,记得把事件传过去]);

service.addEventListener(FaultEvent.FAULT,[失败处理方法,记得把事件传过去]);

var parameter:URLVariables = new URLVariables();

parameter.account = [account];

parameter.password = [password];

service.send(parameter);

在使用URLVariables时碰到一个问题,如果参数名本身就含有选择符“.”,比如:

var parameter:URLVariables = new URLVariables();

parameter.account.system = [account];

parameter.password.system = [password];

运行时会报错如下:

TypeError: Error #1010: 术语尚未定义,并且无任何属性。

at com.adobe.sample::Sample/doLogonSubmit()[F:\[mxml路径]\Sample.mxml:xx]

at flash.events::EventDispatcher/dispatchEventFunction()

at flash.events::EventDispatcher/dispatchEvent()

at mx.core::UIComponent/dispatchEvent()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:13152]

at com.adobe.sample::LogonForm/validate()[F:\[mxml路径]\LogonForm.mxml:xx]

at com.adobe.sample::LogonForm/__logonSubmit_click()[F:\[mxml路径]\LogonForm.mxml:xx]

要传递带选择符的参数,使用如下方法:

var parameter:Object = {"account.system":[account], "password.system": [password]};

好了,这里还要做的就是乱码问题,对于中文这样的多字节文字需要编码后传到服务器,编码方式简单介绍如下:

1、escape,对0-255以外的unicode值进行编码时输出%u****格式。

2、encodeURI,将字符串编码为有效的 URI(统一资源标识符)。将完整的 URI 转换为一个字符串,其中除属于一小组基本字符的字符外,其他所有字符都以 UTF-8 

转义序列进行编码。

3、encodeURIComponent:将字符串编码为有效的 URI 组件。将 URI 的子字符串转换为一个字符串,其中除属于非常小的一组基本字符的字符外,其他所有字符都以

UTF-8转义序列进行编码。encodeURIComponent() 函数与 encodeURI() 函数不同,它仅适用于 URI 字符串的一部分(称为 URI 组件)。URI 组件是指出现在某些特殊字符

之间的任何文本,这些特殊字符称为组件分隔符(: / ; 和 ? )。“http”和“www.adobe.com”是常见的 URI 组件示例。

此函数与 encodeURI() 的另一个重要区别是:由于此函数假定它处理的是 URI 组件,因此它会将特殊分隔符字符 (; / ? : @ & = + $ , #) 视为应进行编码的常规文本。

encodeURIComponent是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持。 

具体采用哪种方式需要配合服务器端的解码方式,如果服务器端的解码方式已经固定了,则还可以对编码结果进行替换等处理,如果服务器端的解码方式还没有那就随便了。

原文地址:https://www.cnblogs.com/soundcode/p/3062680.html