06.13Getting data from hosts that require HTTP Authentication with URLRequest in AS3
Hey Everyone,
just a quick note on getting data from a host that requires HTTP Authentication.. First:
- It seems to only work with POST
- If your server is in PHP (probably other languages too), GET or POST is the same thing, doesn't matter. You can have bogus post data and still pass GET data in the URL.
- You will require a library to encode in Base64, I suggest this one: http://crypto.hurlant.com/ it contains the class
com.hurlant.util.Base64that is simple to use! - You can't use this online, it only works in the development environment!
Here is the code:
Actionscript:
-
// the necessary imports..
-
import flash.events.Event;
-
-
import flash.net.URLLoader;
-
import flash.net.URLRequest;
-
-
import flash.net.URLRequestHeader;
-
import flash.net.URLRequestMethod;
-
-
import flash.net.URLLoaderDataFormat;
-
import com.hurlant.util.Base64;
-
-
// first build the URLRequest
-
var request:URLRequest = new URLRequest("http://your.host.here.com/blahblah?var=value&joke=untrue");
-
-
// the credentials
-
var credentials:String = Base64.encode("username:password");
-
-
// the header to add..
-
var authHeader:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + credentials);
-
-
// create an array to contain the header
-
var headers:Array = [];
-
-
// push your authHeader in there
-
headers.push(authHeader);
-
-
// set the headers on the URLRequest object
-
request.requestHeaders = headers;
-
-
// set some bogus data (otherwise the POST will not happen)
-
request.data = "aaskjdhaskdajhdk";
-
-
// set the request's method to POST
-
request.method = URLRequestMethod.POST;
-
-
// build the loader..
-
var loader:URLLoader = new URLLoader();
-
-
// set the format to text (or whatever you want. URLRequest can be used with other objects too)
-
loader.dataFormat = URLLoaderDataFormat.TEXT;
-
-
// set your handler once the data is in
-
loader.addEventListener(Event.COMPLETE, doComplete);
-
-
// load the request..
-
loader.load(request);
That's it folks!

hello martin .
is it possible to receive (and add ) data from google contact Api with this technique ?
http://code.google.com/apis/gdata/basics.html
thanks
June 26th, 2008 at 1:20 am
What would need to be done to do authentication online?
August 14th, 2008 at 7:03 am
Thing is, you can’t. The security model of Flash disables you to do that, as far as I can understand. This is not very well documented.
August 15th, 2008 at 10:38 am
Ah, I found this post:
http://kb2.adobe.com/cps/403/kb403184.html
March 11th, 2010 at 4:43 pm