HTTPClient every time returns the same string
I tried everything, this one worked for me. Just in case some is not able to make it work:
var uri = new Uri("http://192.168.4.160:8081/v?time=" + DateTime.Now);
If you are using Windows.Web.Http.HttpClient
, you can skip the local cache this way:
Windows.Web.Http.Filters.HttpBaseProtocolFilter filter =
new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
filter.CacheControl.ReadBehavior =
Windows.Web.Http.Filters.HttpCacheReadBehavior.MostRecent;
HttpClient client = new HttpClient(filter);
Uri uri = new Uri("http://example.com");
HttpResponseMessage response = await client.GetAsync(uri);
response.EnsureSuccessStatusCode();
string str = await response.Content.ReadAsStringAsync();
You will never get the same response twice again :)
But if you have access to the server source code, the most elegant fix would be to disable the cache for the URI you are downloading, i.e., add the Cache-Control: no-cache
header.
It's because you're doing a GET to the same URL. According to the HTTP semantics, the value should be the same within a reasonable timeframe, so the OS is caching the response for you.
You can bypass the cache by any of these methods:
- Using a POST request.
- Adding a query string parameter that is different for each call.
- Specifying (on the server) response headers that disable or limit the caching allowed.