Flex构建REST的客户端(两种解决方案)

如果说这篇文章可以帮助你,那么我将很荣幸,呵呵

[size=large]先了解下REST的操作方法:[/size]
Create a new resource:
POST http://www.myserver.org/myResource (with the new contents of the resource as the POSTed payload)

Get the resource with ID=2:
GET http://www.myserver.org/myResource/2

Update the resource with ID=2
PUT http://www.myserver.org/myResource/2 (with the new contents of the resource as the PUT payload)

Delete the resource with ID=2
DELETE http://www.myserver.org/myResource/2
[size=large]解决方案[/size][color=red][/color]
[color=red]方案一:RestHttpService[/color]
下载SWC路径:[url]http://code.google.com/p/resthttpservice/downloads/list[/url]
将SWC添加到项目方法
查看我上一篇文章:[url]http://yiranwuqing.iteye.com/admin/blogs/715413[/url]

使用方法,将SWC添加到项目中后,可以在页面中直接使用
<rest:RestHttpService
id="postHttpService"
host="localhost"
port="8080"
method="{RestHttpService.METHOD_POST}"
path="/myResource"
contentType="application/xml"
result="myResultHandler(event)"
fault="myFaultHandler(event)"/>
触发方法:
postHttpService.send(<myData>This is my data</myData>);
一个具体的例子:[url]http://code.google.com/p/resthttpservice/wiki/Introduction[/url]

[color=red]方案二:as3httpclientlib.[/color]
as3httpclientlib
下载地址:[url]http://code.google.com/p/as3httpclientlib/download/list[/url]
例子:
var client:HttpClient = new HttpClient();
var uri:URI = new URI("http://some.host/");

client.listener.onData = function(event:HttpDataEvent):void {
// Notified with response content in event.bytes as it streams in
};

client.listener.onComplete = function(event:HttpResponseEvent):void {
// Notified when complete (after status and data)
};
//如果是post
var json:String = "{'foo':'bar'}";
var jsonData:ByteArray = new ByteArray();
jsonData.writeUTFBytes(json);
jsonData.position = 0;
var contentType:String = "application/json";

client.post(uri, jsonData, contentType);//post
//如果是multipart
var multipart:Multipart = new Multipart([
new Part("key", objectName),
new Part("Content-Type", contentType),
new Part("AWSAccessKeyId", accessKey),
new Part("Policy", policy),
new Part("Signature", signature),
new Part("file", data, contentType, [ { name:"filename", value:objectName } ]),
new Part("submit", "Upload")
]);
client.postMultipart(uri, multipart);
//如果是Head
client.head(uri);
//如果是del
client.del(uri);
例子链接:[url]http://code.google.com/p/as3httpclientlib/wiki/Examples[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值