在使用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(); } [...]








