为什么异步方法只在此处执行一次HTTP请求?

时间:2013-12-08 19:41:31

标签: c# wpf windows-8.1 windows-store dotnet-httpclient

简而言之,这是一个Windows应用商店应用程序,在Visual Studio 2013中使用 F5 运行时无法生成多个HTTP请求。目标平台是Windows 8.1。

每次单击一个按钮时,都会从公共服务器获取请求时间信息并显示响应。但是,正如Fiddler所示,尽管相关的点击事件处理程序一次又一次地运行,但实际的HTTP请求仅针对第一次点击。

我经历了API docs和其他几个地方,但仍然没有。也许我忽略了某种配置问题,但我无法想象它会是什么。也许这里有人可以吗?

MainPage.xaml.cs中

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Web.Http;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace TestWindowsStore
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private readonly Uri timeInfoHost = new Uri("http://jsontime-sharpnet.rhcloud.com/");
        private readonly HttpClient httpClient = new HttpClient();

        public MainPage()
        {
            this.InitializeComponent();

            var headers = httpClient.DefaultRequestHeaders;
            headers.UserAgent.ParseAdd("ie");
            headers.UserAgent.ParseAdd("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            TimeInfo ti = await PerformTimeRequest();
            this.TimeText.Text = ti.datetime;
        }

        private async Task<TimeInfo> PerformTimeRequest()
        {
            string json = await httpClient.GetStringAsync(timeInfoHost);
            return Deserialize<TimeInfo>(json);
        }

        public T Deserialize<T>(string json)
        {
            var bytes = Encoding.Unicode.GetBytes(json);
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                var jser = new DataContractJsonSerializer(typeof(T));
                return (T) jser.ReadObject(ms);
            }
        }
    }

    [DataContract]
    public class TimeInfo
    {
        [DataMember]
        public string tz;
        [DataMember]
        public string hour;
        [DataMember]
        public string datetime;
        [DataMember]
        public string second;
        [DataMember]
        public string error;
        [DataMember]
        public string minute;
    }
}

MainPage.xaml中

<Page
x:Class="TestWindowsStore.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestWindowsStore"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBox Name="TimeText" HorizontalAlignment="Left" Margin="491,242,0,0" TextWrapping="Wrap" VerticalAlignment="Top" IsReadOnly="True" Width="144"/>
    <Button Content="Get Time Async" HorizontalAlignment="Left" Margin="648,239,0,0" VerticalAlignment="Top" Click="Button_Click"/>

</Grid>
</Page>

为什么没有后续请求?我该如何解决?

1 个答案:

答案 0 :(得分:8)

此处仅执行一次请求的原因是因为默认情况下HttpClient使用缓存来根据收到的Cache-Control标头存储响应(类似于HTTPClient Caching in .Net)。有两种方法可以解决它。

最简单的解决方案是使用HttpBaseProtocolFilter.CacheControl.ReadBehavior属性来处理要跳过缓存的情况:

var httpFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
httpFilter.CacheControl.ReadBehavior = 
    Windows.Web.Http.Filters.HttpCacheReadBehavior.MostRecent;
var httpClient = new Windows.Web.Http.HttpClient(httpFilter);

但是,我认为更清洁的解决方案是修改服务器,使其包含适当的标题,告诉浏览器不要缓存响应,即:

Cache-Control: no-cache

E.g:

HTTP/1.1 200 OK
Content-Length: 31
Content-Type: text/plain; charset=UTF-8
Cache-Control: no-cache

... 

第二种解决方案有两个优点:

  • 您不会再次使用的内容不会存储在客户端计算机中。
  • 其他请求可以从缓存中受益(使用相同的HttpClient实例时)。