HttpClient响应未刷新 [英] HttpClient response not get refresh

查看:184
本文介绍了HttpClient响应未刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HttpClient与Web服务进行通信以进行发送/接收(JSON格式的响应).但是我在接收数据时遇到了一些问题.我每5分钟调用一次Web服务,以使用HttpClient刷新Windows Phone上的内容,但是响应一次又一次地出现.为了获得新的响应,我需要退出应用程序并再次调用服务. HttpClient是否需要一些刷新或清除数据过程?我是否需要实施其他方法来每次都获得新的刷新结果? 请提出建议.下面是我的实现

I am using HttpClient to communicate with web service for send/receive (response in JSON format). But I am facing some issue while receiving data. I am calling web service every 5 min to refresh the content at my windows phone using HttpClient but the response coming same again and again. For getting new response I need to exit from application and call service again. Is HttpClient need some refresh or clear data process? Do I need to implement some other approch to get new refreshed result every time? Please suggest. Below is my implementation

public async Task<string> GetMyData(string urlToCall)
    {
        try
        {
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = TimeSpan.FromMilliseconds(double.Parse(30000));
                HttpRequestMessage request = new HttpRequestMessage(System.Net.Http.HttpMethod.Get, urlToCall);
                var response = await httpClient.SendAsync(request);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var responseString = await response.Content.ReadAsStringAsync();
                    return responseString;
                }
                else
                {
                    return string.Empty;
                }                    
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Exception => RestfulWebService =>GetMyData: " + ex.Message);
            return string.Empty;
        }
    }

推荐答案

Windows Phone(与Windows Store Apps不同)进行了优化,以减少到服务器的呼叫次数.

Windows Phone (unlike Windows Store Apps) has an optimization to reduce the number of calls to the server.

因此,默认情况下,对Phone中的get请求(或GetAsync)进行SendAsync的实现方式是,如果再次进行相同的get请求调用,它甚至不会到达服务器,而是直接返回它的前一个结果.为您而来.

So by default SendAsync for a get request(Or GetAsync) in Phone is implemented in such a way that if you are making the same get request call again, it will not even hit the server but will directly return the previous result it fetched for you.

如果要手动覆盖此优化,则可以将标头"IfModifiedSince"手动添加到请求中,并将datetimeoffset设置为now.这将使客户端每次都再次调用服务器.

If you want to manually override this optimization, you have manually add the header "IfModifiedSince" to the request and set the datetimeoffset to now. This will make the client call the server again each time.

这是代码

      HttpRequestMessage request = new HttpRequestMessage(System.Net.Http.HttpMethod.Get, urlToCall);
      request.Headers.IfModifiedSince = new DateTimeOffset(DateTime.Now);
      var response = await httpClient.SendAsync(request);

请注意,在创建请求之后以及在将其发送到服务器之前,我已在代码中间添加了一行.

Notice that I have added one line in the middle of your code, after creating your request and before sending it to the server.

希望有帮助.

这篇关于HttpClient响应未刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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