9

Could some one make me clear why my code returns the same string every time?

public MainPage()
{
    this.InitializeComponent();

    DispatcherTimer timer = new DispatcherTimer();
    timer.Interval = TimeSpan.FromSeconds(5);
    timer.Tick += OnTimerTick;
    timer.Start();
}

private void OnTimerTick(object sender, object e)
{
    getData();
    HubText.Text = dumpstr;
}

private async void getData()
{
    // Create an HttpClient instance
    HttpClient client = new HttpClient();
    var uri = new Uri("http://192.168.4.160:8081/v");
    try
    {
        // Send a request asynchronously continue when complete
        HttpResponseMessage response = await client.GetAsync(uri);
        // Check that response was successful or throw exception
        response.EnsureSuccessStatusCode();
        // Read response asynchronously
        dumpstr = await response.Content.ReadAsStringAsync();
    }
    catch (Exception e)
    {
        //throw;
    }
}
string dumpstr;

So every 5 seconds I get the same string that I got in my first request. What am I doing wrong?

Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
SABlyu
  • 93
  • 1
  • 3
  • 1
    Well you're not actually waiting until you've *got* your data before you display it. You would be better off making your `getData()` method an async `Task` method, then your timer tick handler could be async as well, with a body of `HubText.Text = await getData();`. Currently I'd expect you to see the next value 5 seconds too late. But as you haven't told us anything about what the URL is meant to return, we don't know why it would change. – Jon Skeet Feb 08 '14 at 08:28
  • I found one more way: to use System.Net.Http against Windows.Net.Http; – SABlyu Feb 10 '14 at 03:14

3 Answers3

16

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.

kiewic
  • 15,852
  • 13
  • 78
  • 101
  • The default HttpCacheReadBehavior applies RFC 2616, but I like to see the HTTP 304 come back from the server, so you need to configure that part: https://msdn.microsoft.com/library/windows/apps/windows.web.http.filters.httpcachereadbehavior.aspx – a7drew Dec 06 '16 at 20:37
12

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.
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
1

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);

pvma
  • 403
  • 8
  • 14