WP8.1 HttpClient 在第一次请求后不下载字符串
WP8.1 HttpClient doesn't download string after first request
这几天我一直在想这是怎么回事。我正在为 Windows Phone 8.1 编写一个应用程序,它使用 HttpClient 从 Internet 下载 json 字符串。它工作正常但仅在 运行 应用程序之后(在 OnNavigatedTo 事件中)一次。后来,再次粉碎应该下载字符串的 'Refresh' 按钮不起作用。它仍然是与第一次下载相同的字符串值。在服务器上,这个字符串发生了变化,我可以通过在 PC 上的浏览器中查看它来确认。
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace WP8App
{
public sealed partial class MainPage : Page
{
private readonly Uri Website = new Uri("https://some-website.com/files/status.json");
private HttpClient http = new HttpClient();
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Refresh();
}
private async void Button_Refresh_Click(object sender, RoutedEventArgs e)
{
Refresh();
}
private async void Refresh()
{
var response = await http.GetStringAsync(Website);
JsonObject api = JsonConvert.DeserializeObject<JsonObject>(response);
TextBox_A.Text = api.value;
TextBox_B.Text = api.length;
TextBox_C.Text = api.size;
TextBox_F.Text = api.volume;
}
private async void ShowDialog(string value)
{
MessageDialog box = new MessageDialog(value);
await box.ShowAsync();
}
}
}
我不是异步高手,所以我指望你的鹰眼 xD 谢谢
WindowsPhone 上的请求缓存非常激进。绕过它的最简单方法是在 URL 的末尾添加一个随机参数。类似于:
private readonly string Website = "https://some-website.com/files/status.json?nocache={0}";
然后每次使用时更改值。使用当前时间戳是确保 URL 始终不同的一种好而廉价的方法:
var response = await http.GetStringAsync(new Uri(string.Format(Website, DateTime.UtcNow.Ticks));
这几天我一直在想这是怎么回事。我正在为 Windows Phone 8.1 编写一个应用程序,它使用 HttpClient 从 Internet 下载 json 字符串。它工作正常但仅在 运行 应用程序之后(在 OnNavigatedTo 事件中)一次。后来,再次粉碎应该下载字符串的 'Refresh' 按钮不起作用。它仍然是与第一次下载相同的字符串值。在服务器上,这个字符串发生了变化,我可以通过在 PC 上的浏览器中查看它来确认。
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace WP8App
{
public sealed partial class MainPage : Page
{
private readonly Uri Website = new Uri("https://some-website.com/files/status.json");
private HttpClient http = new HttpClient();
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Refresh();
}
private async void Button_Refresh_Click(object sender, RoutedEventArgs e)
{
Refresh();
}
private async void Refresh()
{
var response = await http.GetStringAsync(Website);
JsonObject api = JsonConvert.DeserializeObject<JsonObject>(response);
TextBox_A.Text = api.value;
TextBox_B.Text = api.length;
TextBox_C.Text = api.size;
TextBox_F.Text = api.volume;
}
private async void ShowDialog(string value)
{
MessageDialog box = new MessageDialog(value);
await box.ShowAsync();
}
}
}
我不是异步高手,所以我指望你的鹰眼 xD 谢谢
WindowsPhone 上的请求缓存非常激进。绕过它的最简单方法是在 URL 的末尾添加一个随机参数。类似于:
private readonly string Website = "https://some-website.com/files/status.json?nocache={0}";
然后每次使用时更改值。使用当前时间戳是确保 URL 始终不同的一种好而廉价的方法:
var response = await http.GetStringAsync(new Uri(string.Format(Website, DateTime.UtcNow.Ticks));