I did some research and I think I know the answer but I’ll ask anyways hoping for some miracle.
Is there a way to have a synchronous call to a URL to retrieve the HTML code? If not, is it possible to turn an asynchronous call into a synchronous one using some trick? I can’t believe Adobe wouldn’t implement this option. I tried looping until the result is back but then the status function never fires making my loop infinite.
ranting.turn(“ON”);
I’m a ColdFusion developer and recently started working on a project in Flex. I am getting more and more frustrated with AS and Flex lately. It seems that whatever I’m trying to do is either really hard or impossible while it usually can be done with one line of code in ColdFusion. I’m still learning these languages and I’m sure my lack of knowledge is the reason but for some reason I can’t get ahead with my project. It looks like the learning curve is much steeper than it should be. Maybe I just haven’t found the right book/tutorial/site yet.
ranting.turn(“OFF”);
Anyways, here’s my code for testing this functionality. The main application file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init()">
<mx:Script>
<![CDATA[
import myObjects.Domain;
public function init():void
{
var myDomain:Domain = new Domain();
trace("Final result: " + myDomain.testURL("http://test.com"));
}
]]>
</mx:Script>
</mx:WindowedApplication>
and the Domain object looks like this:
package myObjects
{
import flash.events.HTTPStatusEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class Domain
{
private var request:URLRequest;
private var responseCode:int = 0;
public function Domain()
{
}
public function testURL(url:String):Boolean
{
request = new URLRequest(url);
var loader:URLLoader;
loader = new URLLoader();
loader.load(request);
loader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, onStatus);
//while(responseCode == 0) {};
if(responseCode == 200)
{
return true;
}
else
{
return false;
}
}
private function onStatus(e:HTTPStatusEvent):void
{
if(e.status == 200 && url2Domain(e.responseURL) == url2Domain(request.url))
{
responseCode = 200;
trace("OK");
}
else if(e.status != 200)
{
responseCode = e.status;
trace("ERROR: Response code: " + e.status);
}
else
{
responseCode = 404;
trace("ERROR: Response for " + url2Domain(request.url) + " was returned from " + url2Domain(e.responseURL));
}
}
private function url2Domain(url:String):String
{
return url.toLowerCase().replace(new RegExp("^(https?://)?(www\.)?"), "").split("/")[0];
}
}
}
This works OK except the function returns false before the HTTP call returns its result. If I un-comment the while loop the onStatus function will never be called and therefore end up with an infinite loop. Not sure why this happens.
Please let me know if you have any ideas on how to make synchronous calls or have a trick that waits for the result. Appreciate any help.