Actionscript – Flex 3 – how to support HTTP Authentication URLRequest


I have a Flex file upload script that uses URLRequest to upload files to a server. I want to add support for http authentication (password protected directories on the server), but I don't know how to implement this – I assume I need to extend the class somehow, but on how to I'm a little lost.

I tried to modify the following (replacing HTTPService with URLRequest), but that didn't work.

private function authAndSend(service:HTTPService):void{        
   var encoder:Base64Encoder = new Base64Encoder();        
   service.headers = {Authorization:"Basic " + encoder.toString()};

I should point out that I'm not knowledgeable when it comes to ActionScript / Flex, although I have managed to successfully modify the upload script somewhat.

[Edit] – here is an update of my progress, based on the answer below, although I still cannot get this to work:

Thank you for your assistance. I've tried to implement your code but I've not had any luck.

The general behaviour I'm experiencing when dealing with HTTP authenticated locations is that with IE7 all is well but in Firefox when I attempt to upload a file to the server it displays an HTTP authentication prompt – which even if given the correct details, simply stalls the upload process.

I believe the reason IE7 is ok is down to the the session / authentication information being shared by the browser and the Flash component – however, in Firefox this is not the case and I experience the above behaviour.

Here is my updated upload function, incorporating your changes:

private function pergress():void 
if (fileCollection.length == 0) 
  var urlString:String = "upload_process.php?folder="+folderId+"&type="+uploadType+"&feid="+formElementId+"&filetotal="+fileTotal;
  if (ExternalInterface.available) 
    {"uploadComplete", urlString);
if (fileCollection.length > 0) 
  var urlRequest:URLRequest = new URLRequest("upload_file.php?folder="+folderId+"&type="+uploadType+"&feid="+formElementId+"&obfuscate="+obfuscateHash+"&sessidpass="+sessionPass);
  urlRequest.method = URLRequestMethod.POST; = new URLVariables("name=Bryn+Jones");
  var encoder:Base64Encoder = new Base64Encoder();
  var credsHeader:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + encoder.toString());

  file = FileReference(fileCollection.getItemAt(0));
  file.addEventListener(Event.COMPLETE, completeHandler);
  file.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);
  file.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);

As stated above, I seem to be experiencing the same results with or without the amendments to my function.

Can I ask also where the crossdomain.xml should be located – as I do not currently have one and am unsure where to place it.

Best Solution

The syntax is a little different for URLRequest, but the idea's the same:

private function doWork():void
    var req:URLRequest = new URLRequest("");
    req.method = URLRequestMethod.POST; = new URLVariables("name=John+Doe");

    var encoder:Base64Encoder = new Base64Encoder();        

    var credsHeader:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + encoder.toString());

    var loader:URLLoader = new URLLoader();

A couple of things to keep in mind:

  • Best I can tell, for some reason, this only works where request method is POST; the headers don't get set with GET requests.

  • Interestingly, it also fails unless at least one URLVariables name-value pair gets packaged with the request, as indicated above. That's why many of the examples you see out there (including mine) attach "name=John+Doe" -- it's just a placeholder for some data that URLRequest seems to require when setting any custom HTTP headers. Without it, even a properly authenticated POST request will also fail.

  • Apparently, Flash player version completely blocks all Authorization headers (more information on this one here), so you'll probably want to keep that in mind, too.

  • You'll almost surely have to modify your crossdomain.xml file to accommodate the header(s) you're going to be sending. In my case, I'm using this, which is a rather wide-open policy file in that it accepts from any domain, so in your case, you might want to limit things a bit more, depending on how security-conscious you are.


<?xml version="1.0"?>
    <allow-access-from domain="*" />
    <allow-http-request-headers-from domain="*" headers="Authorization" />

... and that seems to work; more information on this one is available from Adobe here).

The code above was tested with Flash player 10 (with debug & release SWFs), so it should work for you, but I wanted to update my original post to include all this extra info in case you run into any issues, as the chances seem (sadly) likely that you will.

Hope it helps! Good luck. I'll keep an eye out for comments.