HttpWebRequest and HttpWebResponse shows old data

微笑、不失礼 提交于 2019-11-28 06:06:49

问题


After updating the data, when the webservice is called, it still fetches old data. New data is loaded only when I logout of the app and then login again.

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 - using C# and XAML

The GetResponseAsync is as follows:

  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;

回答1:


I am facing same problem, and i solved by add response header in my server side like this..

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

may be this will help you too.




回答2:


It seems like HttpWebRequest is returning cached result. You got a few ways to avoid that:

  1. Add a random string to the URL, so a different URL is accessed every time (so www.example.com/page becomes www.example.com/page?random=dsa$fds21).
  2. Disable the response cache, see new code to be added to OnNavigatedTo:

-

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



回答3:


If your using your own API try turning caching off from that side.



来源:https://stackoverflow.com/questions/18288744/httpwebrequest-and-httpwebresponse-shows-old-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!