Using the onHTTPStatus handler
Listen to this article
:: Talkr
Feature
- httpStatus
Level - Intermediate
Audience - Developers
What is HTTP status?
In general a HTTP status is received by the browser from the server when a URL is requested. In case of Flash, the Flash player recieves a HTTP status from the server when a call is made. HTTP status messages need not be always errors. W3C clasifies HTTP status into 5 different categories based on the types of status. You can find the different types of status here.
Use of HTTP status
There are many uses of HTTP status but the primary use is to debug the application. Based on the HTTP status that is received by your application you can write code to handle a case. This is particulary crusial when creating RIA's using Flash. For instance if you are writing a application which aggregates data from different RSS feeds and if some of the servers are down then your application may not work as intended and you would spend a lot of time debugging your application, whereas the actual problem is in the server side and not with your applications. In these kind of situations HTTP status methods comes handy and can save you a lot of development time.
Flash &HTTP status
httpStaus is a new method in Flash 8 which provides a way to get the HTTP status code when trying to load data into Flash. httpStatus can be used mainly with XML and LoadVars.
LoadVars.onHTTPStatus
XML.onHTTPStatus
In the past there was no method in Flash by which you can do this except for the Log class which is used with WebServices.
Usage:
The onHTTPStatus handler is called before the onData call. The onData handler will pass undefined to onLoad handler if the loading fails .
Example:
#include "httpCodes.as"var myXml:XML = new XML();
myXml.onHTTPStatus = function(httpStatus:Number) {
for (var i=0; i<codeArray.length; i++)
{
var errString:String;
errString = httpStatus.toString();
if (codeArray[i].indexOf(errString)>-1)
{
trace(codeArray[i])
}
}
}myXml.load("http://www.lastashero.com/blog/index.xml");
The file httpCodes.as contains the various HTTP status codes and a very brief status message. You can download the file httpCodes.as here.
Just copy and paste the above code in your main timeline and compile the movie. The output window will display the message "200 - OK", which implies that the request has succeeded.
Now for a negative case :
Copy & paste the same code above but modify the last line from : myXml.load("http://www.lastashero.com/blog/index.xml"); to myXml.load("http://www.lastashero.com/blog/index1.xml");
Now when you compile the movie, since index1.xml does not exists on the server your output window will display the message "404 - Not Found". Which is the famous Page Not Found error.
If you want to take decisions based on the type of HTTP status, you can use the code which comes with the examples in Flash 8. So if the status is Information or Success messages you can trivially ignore them and if they are error messages, you can take corrective measures based on the error code.
Additional Resources :
LoadVars.onHTTPStatus
XML.onHTTPStatus
Sample:
This SWF queries various feeds listed in MXNA and returns the status. This is a quick example which I constructed and the code is not optimized, once I come up with a proper example I will post the file here.
Questions & Comments
Have you read this documentation page? If not, you should...
http://livedocs.macromedia.com/flash/8/main/wwhelp/wwhimpl/js/html/wwhelp.htm?href=00002333.html
I´ve also been working with the HTTPstatus handler but noticed very soon the differences between IE & Firefox... Mozilla does not support the HTTPstatus handler.
It´s very sad to see that there are differences between the players until version 8...
Greetings from Germany
Erik
Posted by: Erik Lembke | January 4, 2006 02:34 AM