您的位置:首页 > 理论基础 > 计算机网络

AS3使用HTTPService类访问需要HTTP Basic Authentication认证的URL

2011-11-25 09:44 447 查看

AS3使用HTTPService类访问需要HTTP Basic Authentication认证的URL

在使用API的时候,如嘀咕、Twitter、饭否的API,会需要使用用户名和密码登录,一般都是使用HTTP Basic Authentication认证,使用HTTPService类访问这种URL时,需要设置headers,用户名和密码的格式为“username:password”,并且需要Basic64加密,具体代码如下。

import mx.rpc.events.FaultEvent;

import mx.rpc.events.ResultEvent;

import mx.utils.Base64Encoder;

import mx.rpc.http.HTTPService;

URLRequestDefaults.authenticate = false;//设默认为false,否则用户较验错误时会弹出验证框

private var result:XML;

private function initApp():void{

var base64enc:Base64Encoder = new Base64Encoder;

base64enc.encode("user:password"); //用户名和密码需要Base64编码

var user:String = base64enc.toString();

var http:HTTPService = new HTTPService;

http.addEventListener(ResultEvent.RESULT,resultHandler);//监听返回事件

http.addEventListener(FaultEvent.FAULT,faultHandler); //监听失败事件

http.resultFormat = "e4x";//返回格式

http.url = "http://api.digu.com/statuses/friends_timeline.xml"; 以嘀咕网的API为列

http.headers = {"Authorization":"Basic " + user};

http.send();

}

private function resultHandler(e:ResultEvent):void{

result = XML(e.result);

test.dataProvider = result.status;//绑定数据

}

private function faultHandler(e:ResultEvent):void{

//处理失败

}

(来源:http://foxling.org/as-flex-air/as3-http-basic-authentication-with-httpservice-object/)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐