Caching issue with WebClient/HttpClient

2019-01-27 03:51发布

问题:

I'm developing a code to poll a Bitstamp exchange ticker every 30 seconds. This is a code I have:

public IObservable<string> Stream(Uri way, WebClient wc)
    {
        Func<IObserver<string>, Task> Fun = async Observer =>
        {
            var res = await wc.DownloadStringTaskAsync(way);
            Observer.OnNext(value: res);
        };

        return Observable.Create<string>(Fun);
    }

public IObservable<string> GetDelay(int secs)
    {
        var exe = TimeSpan.FromSeconds(secs);
        return Observable.Empty<string>("x").Delay(exe);
    }

Stream(new Uri("https://bitstamp.net/api/ticker"), new WebClient { }).Concat(GetDelay(30))
    .Repeat(5).Subscribe(res => Debug.WriteLine("got result: {0}", res));

The problem is that WebClient (and HttpClient, too) both return cached results after the first call, it can be seen by the same timestamp:

got result: {"high": "690.00", "last": "645.10", "timestamp": "1387715532" ... }
got result: {"high": "690.00", "last": "645.10", "timestamp": "1387715532" ... }
...

Even after turning the networks off they return the result normally so obviously they cache it somewhere. Adding something like "?cache=random" does not work because request parameters are not allowed for ticker on Bitstamp. Setting Headers[HttpRequestHeader.CacheControl] = "no-cache" for WebRequest does not work either.

How can I fix this weird caching behavior?

回答1:

Solved by setting wc.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString(); before each subsequent call.



回答2:

For Windows Phone Setting with Language=Chinese and "date+time" is not "24-hour clock" , the code wc.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString() causes exception throwing since DateTime.ToString() generates Chinese characters "下午" in the header.

The safer solution is to output the date time format as RFC1123 pattern:

req.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString("R");

This will ensure the date time is in format of "Sat, 05 Jul 2014 13:38:28 GMT", and there will no any Chinese characters within the HTTP headers.