HTTP Authentication with Flash

For posterity and the rest of the internet, here is the trick to get flash to load variables using LoadVars on a password protected site, when it’s using http authentication. I have found parts of examples, but not an actual working example, so here it is.

First of all, get the actionscript Base64 encoder class.
Put it in your classpath or the same directory as your .fla file.

Then do something like this:

var loadvars = new LoadVars();
var str = Base64.Encode("username:password");
loadvars.addRequestHeader("Authorization:", "Basic "+str);
loadvars.load("some_password_protected_url.php");

That’s all.