views:

31

answers:

2

Is there a standard class that simply returns the response received from a URL given a URL?

Looking through the documentation, I found mx.servicetags.HTTPService, but I don't think it's what I'm looking for.

Update: The request is happening inside of a function and therefore that same function has to return the result. In other words, callbacks aren't going to work.

+4  A: 

HTTPService is specific to Flex. If you're looking for a pure actionscript-3 solution, use URLLoader with URLRequest.

var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.load(new URLRequest(url));

function onLoad(e:Event):void
{
  var loadedText:String = URLLoader(e.target).data;
  trace(loadedText);
}
Amarghosh
Well, the problem is that the data is being fetched inside a function and that same function needs to return the fetched data.
George Edison
@George The loading process is asynchronous, you can't return the loaded data immediately. Move the remaining part of the calling function to the `onLoad` handler.
Amarghosh
@George you wouldn't want to block until the page was loaded anyway - this would cause your Flash movie to freeze until the page was completely downloaded, which might take a while.
Ender
@Ender: Okay, I guess I'm stuck doing that :)
George Edison
@George Don't forget to handle `ioError` and `securityError` - a detailed example is available in the linked page. You can load text, name-value pairs or binary data (check out `URLLoaderDataFormat` class).
Amarghosh
A: 

see sample code here

RC