in Interfaces -- HTML5/JS/Ajax/iOS/Android/Flex/AIR/PDF

Flex HTTPService, Browser Cache and IE

This morning I spoke at a Flex event in Hyderabad (India). Our (Saven Technologies) team members at Hyderabad planned and organized this fantastic event. It was a public event. Over 200 enthusiatic people attended the event.

I sincerely appreciate the effort of the organizers and thank the attendees for making it a successful event.

One of the attendees asked me a question about problems with Flex HTTPService and the IE browser cache. I promised to provide a detailed solution to the problem, so here it is:

Problem: Repeated HTTPService calls when made from Flex (running within an instance of the IE browser) many a times ends up with no external HTTP call. It appears the data is served from cache.

Reason:  The Flash Player piggybacks on the browser to make the HTTP call. IE caches the response from the HTTP GET calls and on occurrence of the same URL returns the response from the cache.

Solution: The problem can be solved either at the server side or at the client side.

Server side solution:  Set the HTTP headers of the response to avoid returning response from cache.

In HTML: (in the header)

<META HTTP-EQUIV=”Cache-Control” CONTENT=”no-cache”>

In PHP: (in the script)

header(”Cache-Control: no-cache, must-revalidate”);
header(”Expires: Mon, 26 Jul 1997 05:00:00 GMT”);

In JSP: (before writing to the output stream)

response.setDateHeader (“Expires”, 0);

Client side solution: (1) Make HTTP POST call — only HTTP GET calls are served from cache or (2) Make sure the HTTP GET URL is different every time.

(1) Make HTTP POST call —
set method=”POST” and handle the call appropriately

(2) Append a unique parameter to the HTTP GET call so that the URL is different every time. A unique time stamp is a good choice.
The following sample code, may do the job:

var timeStampForNocache:Date = new Date() ;
params.noCacheControlVar = timeStampForNocache.getTime().toString() ;
I have named the parameter “noCacheControlVar”. You can name it anything else you please. The name does not matter. What matters is that the timestamp makes the HTTP GET URL unique.

That’s it! Hope it helps and IE does not trouble you when using HTTPService anymore.

Leave a Reply

  1. Hi,

    We just came accross a serious issue yesterday, where we are using HTTPService to do xmlrpc. We do it via Posts using the as3rpclib (on google code) and found that if we try to do 2 requests in quick succession the first works and the second one returns a fault. Using the Fiddler2 I can see the second and first requests and both are Indeed POSTs, and the both return correct results from the server.

    When debugging I am seeing the result from the second call to trigger a #2032: Stream Error.

    Do you have an idea of any work arounds in this Case?

    Thanks and Regards

  2. thanks a lot¡¡¡¡
    I had spent a lot of time searching this¡¡¡.
    I’ve tried it and it works¡¡¡
    Thanks again.

  3. Hey
    Thanks for this post.
    I’ve tried it, and I worked fine
    but Today while I’m testing my project, it caches again ..
    anything else u suggest to do.

    Thanks in Advance