Over a million developers have joined DZone.

Solving an HTTP Proxy Caching Issue on Windows Phone

· Mobile Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

Recently, I noticed that when I did an HTTP GET to one of my API endpoints at work, the request came back with the same data, no matter what. I knew the data should come back differently, because I would make a change to the backend data and it was not reflected in the response.  To ensure that my API was working as expected, I was able to verify that the response was coming back correctly in Postman, as well as directly from the URL in Chrome.

I suspected my issue had something to do with request caching coming from the same client, because I was making my GET requests back-to-back, within five seconds. To test my theory, I added both a "Cache-Control" and "Pragma" request header to my request and set them both to "no-cache," but that did not help.

Being a bit stumped here, I reached out to our API developers to enlist their help. The first thing they did was start watching the incoming traffic, and they noticed that my request was never actually leaving the device, so there was no way it would ever return the new data. To ensure that this was just an emulator issue, I deployed to my actual phone to see if the issue persisted there, and sure enough, it did.

We now knew that something inside of the Windows Phone was doing HTTP GET request caching. So, how do we fix it?

We figured that we could solve the problem the simple, non-elegant way, which is to tack on a random query string to each request, thus forcing each request to be unique. This did work, but man, did that feel dirty.

After doing a bit of research, I came across the If-Modified-Since request header. The specs state the following:

The If-Modified-Since request-header field is used with a method to make it conditional: if the requested variant has not been modified since the time specified in this field, an entity will not be returned from the server; instead, a 304 (not modified) response will be returned without any message-body.

This should work great. All I needed to do was use this with the current date and time. Sure enough, when I added this request header along with DateTime.Now(), my GET request made it to the server and I was able to get my updated data as expected.

Hope this helps.

The Mobile Zone is brought to you in partnership with Strongloop and IBM.  Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud.


Published at DZone with permission of Derik Whittaker, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}