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

7 篇文章 0 订阅
1 篇文章 0 订阅

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/)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值