Windows Phone 8 HttpClient Get 方法返回奇怪的结果 [英] Windows Phone 8 HttpClient Get method returns strange results

查看:24
本文介绍了Windows Phone 8 HttpClient Get 方法返回奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Windows Phone 8 应用程序,该应用程序将一些数据发送到执行它并返回结果的服务器.可以随时查询服务器以获取当前执行的状态,可以是初始化、正在运行或已完成.仅当执行处于完成状态时,输出才可用.用户可以通过按下更新"按钮来检查执行的当前状态

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>

这是方法

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

所以问题是这个方法只有在第一次被调用时才能正常工作.之后,无论服务器的实际状态如何,GetStringAsync() 都会返回与第一次调用相同的结果.

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.

我曾在一个单独的Windows Phone项目中尝试过这种方法,结果是一样的.为了确保服务器正常运行,我这次在桌面应用程序上再次尝试了相同的 C# 代码,并且运行良好.

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.

我的理论是,因为我多次发送相同的请求,WP OS 正在缓存最后一个结果并将其返回给我,而不是实际向服务器发出 GET 请求.

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.

为什么 HttpClient 返回缓存状态而不是实际从服务器获取状态?

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

推荐答案

正如评论它的人所建议的那样,这是一个缓存问题.来自用户 L.B.设置 client.DefaultRequestHeaders.IfModifiedSince 解决了问题

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

这篇关于Windows Phone 8 HttpClient Get 方法返回奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆