Search code examples
silverlightwindows-phonehttpwebrequest

Windows Phone Silverlight request does not update


I'm quite new to the Windows Phone dev and I have to do an application to communicate with a Restful API. Everything works fine to get the information back from the API but my problem occurs when I try to update the content. For example, I have a profile and I try to update the user's information (change the city let's say). On the server side I can see that my update worked properly but when I go back to my profile in my WP app nothing has changed, the city is still the same as the old one. This is my code :

    public MainPage()
    {
        InitializeComponent();
        this.ApplicationBar = this.Resources["HomeBar"] as ApplicationBar;
        Requester requester = new Requester();
        requester.initGetRequest("/me/", GetResponseCallback, true);
    }

    private void GetResponseCallback(IAsyncResult asynchronousResult)
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
            string read = streamRead.ReadToEnd();
            GlobalData.GetInstance().user = JsonConvert.DeserializeObject<MeClass>(read);

            Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show(read);
            }); 

//Create the profile and stuff

            streamResponse.Close();
            streamRead.Close();
            response.Close();
        }
        catch (WebException webException)
        {
            HttpStatusCode status = ((HttpWebResponse)webException.Response).StatusCode;
            Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show(status.ToString());
            });
        }
    }

I figured out that the string 'read' is always equal to the old one, even after the update so this is why the content is not updated but how can the response be exactly the same as before, even if the update worked fine on the server side (if I check in Postman after my update, I can see that my city is the new one). If I restart my app I can see the update. I can also show you my initGetRequest():

        public void initGetRequest(String endPoint, Action<IAsyncResult> callback, Boolean header)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + endPoint);
        if (header == true)
            request.Headers["Authorization"] = GlobalData.GetInstance().Header;
        request.BeginGetResponse(new AsyncCallback(callback), request);
    }

Solution

  • I finally found why my request was still the same even after the update. The HttpWebRequest uses a cache by default. I only added a small bit of code before calling my request :

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + endPoint);
            if (header == true)
                request.Headers["Authorization"] = GlobalData.GetInstance().Header;
            request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();
            request.BeginGetResponse(new AsyncCallback(callback), request);
    

    I had no idea about that cache so I hope this answer will help someone having the same issue !