2

How can I access a password protected webservice from AS3 (compiled using flash ide, so not flex application)

I tried just calling it straight like http://username:password@webserviceurl.co and it gets rejected.

Also tried using this

var authHeader:URLRequestHeader = new URLRequestHeader("Authorization","Basic " + credentials);
//add the header to request
request.requestHeaders.push(authHeader);

where "Credentials" is the username:password that I just used a website to convert to base64.

That doesn't seem to work either. Not seeing the header get send in the packet watcher.

Am I missing anything?

Tim Cooper
  • 144,163
  • 35
  • 302
  • 261
teepusink
  • 23,746
  • 37
  • 103
  • 147
  • I've asked this question before and whilst I other issues, you might find this useful: http://stackoverflow.com/questions/7752821/does-flash-not-support-username-password-in-a-url-what-are-my-alternatives See: http://stackoverflow.com/questions/509219/flex-3-how-to-support-http-authentication-urlrequest – Hawks Dec 16 '11 at 11:13

1 Answers1

0

Try something like: (untested code)

import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;

    attemptLogin("dude","mypassword");

    function attemptLogin(username:String, pass:String):void {

        var req:URLRequest new URLRequest("https://yoursite.com");
        req.method = URLRequestMethod.POST;
        req.data = "username=" + username + "&password=" + pass;
        var loader:URLLoader = new URLLoader();
        loader.dataFormat = URLLoaderDataFormat.VARIABLES;
        loader.addEventListener(Event.COMPLETE, completeFun);
        loader.addEventListener(IOErrorEvent.IO_ERROR, IOErrorFun);
        loader.load(request);
    }

    function completeFun(evt:Event):void { // Check server response (using evt.target.data)}
    function IOErrorFun(evt:IOErrorEvent):void {}
ToddBFisher
  • 10,800
  • 8
  • 34
  • 49