简体   繁体   中英

Windows Phone 8 HttpClient Get method returns strange results

I am developing a Windows Phone 8 app that sends some data to a server which executes it and returns a result. The server can be queried at any time to GET the status of the current execution which could be initializing,running or finished. The output is available only when the execution is in the finished state. The user has the option to check the current status of the execution, by pressing an 'update' button

XAML

<Button Background="{StaticResource PhoneAccentBrush}" 
        Click="UpdateRunInfo" > Update info</Button>

This is the method

private async void UpdateRunInfo(object sender, RoutedEventArgs e)
    {
        ExecutionItem clicked = ((sender as Button).DataContext as ExecutionItem);
        HttpClientHandler handler = new HttpClientHandler();
        handler.Credentials = new NetworkCredential("username", "password");           
        HttpClient client = new HttpClient(handler);                    
        string Url = "http://somefakeurl.com/server/run/id/status";
        string _status = await client.GetStringAsync(Url);
        clicked.status = _status;
                }

So the problem is that this method work properly only the first time it is called. After that, GetStringAsync() returns the same results as the first call regardless of the actual status of the server.

I have tried this method in a separate Windows Phone project, the result is the same. To be sure that the server is running correctly I tried again the same C# code this time on a desktop application and it works perfectly fine.

My theory is that because I send the same request multiple times the WP OS is caching the last result and it gives it back to me instead of actually making the GET request to the server.

Why does the HttpClient return a cached status instead of actually getting the status from the server ?

As suggested by the people commenting it was a caching problem. The suggestion from user LB to set client.DefaultRequestHeaders.IfModifiedSince resolved the problem

As already commented, it's a caching problem (simply speaking: your request is cached, so you get the cached response). Alexei's answer is probably the most used, especially using the current time as query parameter.

Note: guys, please answer, not comment, otherwise the question will remain in "unanswered" status.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM