Content webservice call not refreshed in windows p

2019-09-02 03:56发布

问题:

Currently, I'm developing a Windows Phone 8.0 application that displays some 100 GPS locations on a map. The GPS data is obtained by calling a RESTful webservice as follows:

public async Task<string> GetWebContent(string uri)
{
   string result = null;
   using (var client = new HttpClient())
   {
      result = await client.GetStringAsync(new Uri(uri));        
   }

   return result;
}

Here, the HttpClient is taken from the Nuget Package "Microsoft HTTP Client Libraries".

The first time the code is called, data is obtained and the GPS locations are nicely plotted on a map. However, since the GPS devices move constantly, I have to refresh the data every 30 seconds. This is where I hit the wall; the data contained in the variable "result" never changes. Even after several minutes the data is the same. The raw data contain a datetime property, which clearly tells me that the data is old and not just the GPS devices standing still. If I paste the uri into a webbrowser, and hit the refresh butten every 15 sec, the data does changed hence the webservice is working properly.

Since the HttpClient is contained in a using-statement, it is disposed off every time and some hidden caching mechanism seems impossible. So, does anyone have an idea?

cheers

Update

I tried using Fiddler, but had to follow a lengthy instruction to make it connect with the emulator. Once done, nothing worked anymore :-s Reverting all the certificates that Fiddler installed and copied the code to a regular WPF application and there it works als a charm.I'm no closer to an understanding of what's going on, but the problem seems to be related to the fact that it's a windows phone project.

回答1:

It maybe caching the result from the first call. You may need to set the IfModifiedSince property in the request header before each subsequent call to ensure that its getting the latest value.

Example code from: https://stackoverflow.com/a/17884734/61226

client.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow;

See similar issues here:

  • No cache with HttpClient in Windows Phone 8
  • Caching issue with WebClient/HttpClient