سؤال

I have a problem in my app with the HttpClient. When I change the date or time on my phone the HttpClient cache gets messed up. For example, when I change the date backwards and run the app again every response is taken from the cache, and when I move the date forward, no request is cached, nor taken from the cache. I don't know where the problem is, so help if you encountered a similiar problem.

Is there any way to clear the HttpClient cache programatically? I would do that on every app start.

EDIT: I just used Fiddler to look at responses and found out that the time in the 'Date' response header is the correct time (on server), and maybe HttpClient uses this header for its calculations in combination with the time on the device, which returns wrong results

EDIT2: One use case: When the user opens the app and fetches an url (http://my.website.com/jsondata) where the cache control header is set to 10 seconds, and then closes the app and changes the time backwards or forwards, and then opens the app again in some time, then in the first case the httpclient Always gets the response of the same url from its cache, and in the second case never uses the cache again (if the user requests the same url multiple times in 10 seconds)

My code is :

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;
    }
هل كانت مفيدة؟

المحلول

You can try to add one more parameter to your input queries in URL. You can follow my another post: Why does the HttpClient always give me the same response?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top