Windows Phone 中的内容 Web 服务调用未刷新

问题描述:

目前,我正在开发一个 Windows Phone 8.0 应用程序,可在地图上显示大约 100 个 GPS 位置.GPS 数据是通过调用 RESTful webservice 获取的,如下所示:

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;
}

这里,HttpClient 取自 Nuget 包Microsoft HTTP 客户端库".

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

第一次调用代码时,会获得数据,并且 GPS 位置很好地绘制在地图上.但是,由于 GPS 设备不断移动,我必须每 30 秒刷新一次数据.这是我撞墙的地方;变量result"中包含的数据永远不会改变.即使几分钟后,数据还是一样的.原始数据包含一个 datetime 属性,它清楚地告诉我数据是旧的,而不仅仅是静止不动的 GPS 设备.如果我将 uri 粘贴到网络浏览器中,并每 15 秒点击一次刷新按钮,数据确实会发生变化,因此网络服务可以正常工作.

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.

由于 HttpClient 包含在 using 语句中,因此每次都将其丢弃,并且某些隐藏的缓存机制似乎是不可能的.那么,有人有想法吗?

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?

干杯

更新

我尝试使用 Fiddler,但必须遵循冗长​​的说明才能使其与模拟器连接.完成后,什么都没有了:-s 恢复 Fiddler 安装的所有证书并将代码复制到常规 WPF 应用程序,它在那里工作也是一个魅力.我没有更深入地了解正在发生的事情,但问题似乎与它是一个 Windows 手机项目有关.

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.

它可能会缓存第一次调用的结果.您可能需要在每次后续调用之前在请求标头中设置 IfModifiedSince 属性,以确保其获取最新值.

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.

示例代码来自:https://*.com/a/17884734/61226

client.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow;

在此处查看类似问题: