Browser cache issue with Flex HTTPService

I struggled to figure this one out, whcih was causing lots of issues in my application. I was using one httpservice to call one URL which will return one xml. This xml in turn forms the dataprovider for one datagrid. The xml is generated based on some selections, but after loading for the first time, after the selection changes, the httpservice was not returning the new xml, in turn it provides the same XML. Hence the datagrid was not getting refreshed. From first view itself I guessed the problem is with caching, since it works well each time I clear the IE cache. Finally the solution is here :

Problem: Repeated HTTPService calls when made from Flex 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 JSP: (before writing to the output stream)response.setHeader(“Cache-Control”,”no-cache”);
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”

(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 params:Object = {};
var noCache:Date = new Date() ;
params.noCache = noCache.getTime().toString() ;

I have named the parameter “noCache”. 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 guyss.. Enjoy and Happy Coding.. Cheers.


Tags: , , , , , ,

7 Responses to “Browser cache issue with Flex HTTPService”

  1. Vijay Singh Says:

    Nice Blog…..keep it up.

  2. Lars Says:

    looking a long time for an explanation for this behavior and how to fix it!

  3. Scott Griz Says:

    We spent two hours trying to resolve this. Thanks for posting this. You’re a big life saver. We used new system generated parameter because our client didn’t allow us to change the IE settings.

    var params:Object = new Object();
    var tempDate:Date = new Date();
    params.newDate =tempDate;

    • Anoop Says:

      Yeah Scott, I spent nearly 1-2 days to solve this issue and this is the main aim of my blog. To help others to save time for small small issues like this, where I have spent some time upon. Thanks for visiting my blog and keep coming 🙂

  4. Scott Griz Says:

    Also, Thanks for allowing to post as a guest!

  5. Amith Says:

    in a Flex project how do i solve similar issue? any idea pls.

    • Anoop Says:

      Amith, the scenario i had blogged is for Flex project itself. I was using HTTPService to connect to service. caching is always a headache for HTTP calls. For Flex project also you can follow the same situation. Or if you could tell me the precise problem, I can take a look on that. 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: