HttpWebRequest和HttpWebResponse显示旧数据

时间:2013-08-17 12:18:52

标签: c# windows-phone-8 httpwebrequest streamreader httpwebresponse

更新数据后,当调用webservice时,它仍然会获取旧数据。只有在我注销应用程序然后再次登录时才会加载新数据。

protected async override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
            base.OnNavigatedTo(e);
            parameterValue = this.NavigationContext.QueryString["parameter"];

            Uri UserDetailUrl = new Uri(Constants.WebService.ws_single_user + "?user_id=" + parameterValue);
            HttpWebRequest UserDetailRequest = (HttpWebRequest)HttpWebRequest.Create(UserDetailUrl);
            HttpWebResponse UserDetailResponse = (HttpWebResponse)await UserDetailRequest.GetResponseAsync();
            StreamReader reader = new StreamReader(UserDetailResponse.GetResponseStream());
            string UserDetailString = reader.ReadToEnd();
            reader.Close();
            XDocument XUserDetailDoc = XDocument.Load(new StringReader(UserDetailString));
            if (((XElement)XUserDetailDoc.Element("main")).Element("result").Value == "success")
            {
                txtEmail.Text = ((XElement)XUserDetailDoc.Element("main")).Element("user").Element("email").Value;
                txtFirstName.Text = ((XElement)XUserDetailDoc.Element("main")).Element("user").Element("fname").Value;
                txtLastName.Text = ((XElement)XUserDetailDoc.Element("main")).Element("user").Element("lname").Value;
                txtMobile.Text = ((XElement)XUserDetailDoc.Element("main")).Element("user").Element("mobile").Value;
            }
    }

Windows 8 Phone App - 使用C#和XAML

GetResponseAsync如下:

  public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request)
    {
        var taskComplete = new TaskCompletionSource<HttpWebResponse>();
        request.BeginGetResponse(asyncResponse =>
        {
            try
            {
                HttpWebRequest responseRequest = (HttpWebRequest)asyncResponse.AsyncState;
                HttpWebResponse someResponse = (HttpWebResponse)responseRequest.EndGetResponse(asyncResponse);
                taskComplete.TrySetResult(someResponse);
            }
            catch (WebException webExc)
            {
                HttpWebResponse failedResponse = (HttpWebResponse)webExc.Response;
                taskComplete.TrySetResult(failedResponse);
            }
        }, request);
        return taskComplete.Task;

3 个答案:

答案 0 :(得分:4)

似乎HttpWebRequest正在返回缓存结果。你有几种方法可以避免这种情况:

  1. 向网址添加随机字符串,因此每次都会访问不同的网址(因此www.example.com/page变为www.example.com/page?random=dsa$fds21)。
  2. 禁用响应缓存,查看要添加到OnNavigatedTo的新代码:
  3. -

    HttpWebRequest UserDetailRequest = (HttpWebRequest)HttpWebRequest.Create(UserDetailUrl); 
    // Define a cache policy for this request only. 
    HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
    request.CachePolicy = noCachePolicy;
    

答案 1 :(得分:2)

我面临同样的问题,我通过在我的服务器端添加响应标头来解决这个问题。

response.setHeader("Cache-Control", "no-cache");

可能会对你有所帮助。

答案 2 :(得分:0)

如果您使用自己的API,请尝试从该方面关闭缓存。