Wednesday, February 06, 2008

Setting Custom HTTP Headers in Flash

I needed to set a custom HTTP header in Flash. ActionScript 3 makes this easy, or so I thought. Here was my original prototype code:

import flash.net.navigateToURL;
private function href(url:String):void
{
var req:URLRequest = new URLRequest(url);
var header:URLRequestHeader = new URLRequestHeader("Koostoom", "tmttos");
req.requestHeaders.push(header);
navigateToURL(req);
}

Like I said, this is easy. To verify it was working, I wrote a quick prototype using PHP:

$headers = getallheaders();
foreach ($headers as $name => $value) {
echo "[$name] = $value<br/>\n";
}

I think I copied this out of the PHP manual pretty much. So I tested the above code and everything worked, right? I wouldn't be writing this post if that was the case!

I tried the above in Firefox and Safari. My usual test plan is to make sure it works in those two browsers first, and then deal with IE. In this case it didn't work in either browser. I knew that some HTTP headers were off-limits in Flash, so just for just kicks I tried changing Koostom to Referrer. This should have caused an exception, but of course it didn't. It did nothing.

Finally, I found the answer through experimentation. You have to do an HTTP POST with form data in order to get the custom header sent:

import flash.net.navigateToURL;
private function href(url:String):void
{
var req:URLRequest = new URLRequest(url);
// begin stupid hack
req.method = "POST";
var formVars:URLVariables = new URLVariables();
formVars.blah = "blue";
req.data = formVars;
// end stupid hack
var header:URLRequestHeader = new URLRequestHeader("Koostoom", "tmttos");
req.requestHeaders.push(header);
navigateToURL(req);
}

Now the PHP script showed my Koostoom header...

I don't know if this is a bug with the Flash player, or the browsers. I don't think it is part of the HTTP spec, i.e. I think custom headers are just as valid in an HTTP GET as in an HTTP POST.

6 comments:

baby mom said...

thank you! I was running out of hair to pull out Your hack did the trick.

Anonymous said...

It seems that if you pass more than about 2 custom headers (it might depend on the overall length but these are short strings), it doesn't send ANYTHING even on a POST. No error, no exception, nothing. And to make it worse - if you add a few more custom headers (maybe 4 or 5), it doesn't seem to even make the call at all, GET or POST! Again, no error indications, no exceptions. Just trash - I expect better from Flash and Adobe. They need to fix or at least explain this better.

Anonymous said...

This is not working in Flash10/AIR1.5/IE7/Windows XP environment.
Is any one else facing this issue?

Anonymous said...

Your solution is actually not a hack at all, here is what Adobe says on this matter, "Due to browser limitations, custom HTTP request headers are only supported for POST requests, not for GET requests."

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequestHeader.html#methodSummary

Viagra Online said...

This should have caused an exception, but of course it didn't. It did nothing just get in my way to try this once again...

Louisville Lodging said...

Wow... what a great idea, You will be in our prayers and thoughts! I like your work..Keep working.