代码之家  ›  专栏  ›  技术社区  ›  Kristian Vukusic

更改日期时的HttpClient缓存

  •  0
  • Kristian Vukusic  · 技术社区  · 11 年前

    我的应用程序有问题 HttpClient 。当我在手机上更改日期或时间时 HTTP客户端 缓存被打乱了。例如,当我向后更改日期并再次运行应用程序时,每个响应都会从缓存中提取,而当我向前移动日期时,不会缓存任何请求,也不会从缓存中获取。我不知道问题在哪里,所以如果你遇到类似的问题,请帮忙。

    有没有办法清除 HTTP客户端 编程缓存?我会在每次应用程序启动时都这样做。

    编辑: 我刚刚使用Fiddler查看了响应,发现“Date”响应头中的时间是正确的时间(在服务器上) HTTP客户端 将此标头与设备上的时间一起用于计算,这会返回错误的结果

    编辑2: 一个用例: 当用户打开应用程序并获取url时( http://my.website.com/jsondata )其中缓存控制头设置为10秒,然后关闭应用程序并向后或向前更改时间,然后在某个时间内再次打开应用程序,在第一种情况下,httpclient总是从其缓存中获取相同url的响应,在第二种情况下决不再使用缓存(如果用户在10秒内多次请求相同的url)

    我的代码是:

    public async Task<StringServerResponse> DownloadJsonAsync(Uri uri, DecompressionMethods decompressionMethod = DecompressionMethods.None, int timeout = 30000, int retries = 3)
        {
    
            if (retries < 1 || retries > 10) retries = 3;
            int currentRetries = 0;
    
            // Baseline delay of 1 second
            int baselineDelayMs = 1000;
            // Used for exponential back-off
            Random random = new Random();
    
            StringServerResponse response = new StringServerResponse();
    
            if (decompressionMethod == DecompressionMethods.GZip)
            {
                 //Use decompression handler
                using (var compressedHttpClientHandler = new HttpClientHandler())
                {
                    if (compressedHttpClientHandler.SupportsAutomaticDecompression)
                    {
                        compressedHttpClientHandler.AutomaticDecompression = System.Net.DecompressionMethods.GZip;
                    }
                    using (var httpClient = new HttpClient(compressedHttpClientHandler))
                    {
                        httpClient.Timeout = TimeSpan.FromMilliseconds(timeout);
    
                        do
                        {
                            ++currentRetries;
                            try
                            {
                                var httpResponse = await httpClient.GetAsync(uri, HttpCompletionOption.ResponseContentRead);
                                if (httpResponse.IsSuccessStatusCode)
                                {
                                    response.StatusCode = httpResponse.StatusCode;
                                    response.Content = await httpResponse.Content.ReadAsStringAsync();
                                    return response;
                                }
                            }
                            catch (Exception)
                            {
                            }
    
                            int delayMs = baselineDelayMs + random.Next((int) (baselineDelayMs*0.5), baselineDelayMs);
                            if (currentRetries < retries)
                            {
                                await Task.Delay(delayMs);
                                DebugLoggingService.Log("Delayed web request " + delayMs + " seconds.");
                            }
    
                            // Increment base-delay time
                            baselineDelayMs *= 2;
    
                        } while (currentRetries < retries);
                    }
                }
            }
            else
            {
                // same but without gzip handler
            }
    
            return null;
        }
    
    1 回复  |  直到 11 年前
        1
  •  0
  •   Community CDub    7 年前

    您可以尝试在URL中的输入查询中再添加一个参数。你可以关注我的另一篇帖子: Why does the HttpClient always give me the same response?