windows-phone-8.1httprequestwindows-rt

Windows Phone 8.1 RT Windows.Web.Http.HttpClient returning same data in different authentication responses


I am creating the Windows Phone 8.1 RT app (VS 2015 Community). I selected the Windows.Web.Http.HttpClient for the communication with web service.

On one page, I am trying to get list of items from the web service with following code:

    public static async void GetItems(string username, string password, Action<ItemsModel, string, string> completion)
    {
            string methodUrl = "methodUrl";
            var resourceUri = new Uri(new Uri(baseUrl), methodUrl);
            var client = new HttpClient();
            var byteArray = System.Text.Encoding.UTF8.GetBytes(username + ":" + password);
            var basicAuth = Windows.Web.Http.Headers.HttpCredentialsHeaderValue("Basic", Convert.ToBase64String(byteArray));

            client.DefaultRequestHeaders.Authorization = basicAuth;

            var response = new HttpResponseMessage();
            try
            {
                response = await client.GetAsync(resourceUri);
            }
            catch (Exception ex)
            {
            }

            if (response.IsSuccessStatusCode)
            {
                var responseContent = await response.Content.ReadAsStringAsync();
                var items = DeserializeMyItems(responseContent);
                completion(items, null, null);
            }
            else
            {
                //handle errors
            }
    }

It all works well when I login (basic auth) with one user credentials. But when I try other user credentials (and every other after it) I still get the same response as for the first one (like response is not refreshed). That is happening for every account I connect with the first time I start the app. After first successful response, I always get the data from that response (even if I login the other valid user credentials).

I've checked the Postman for this request and I there I see that the problem is not in the web service, but on my side.

I've also checked the headers of response and I saw that all following responses have the same id in headers (X-Vcap-Request-Id).

Can somebody point me to the error that is causing this kind of behavior?

Thank you


Solution

  • You can create HttpBaseProtocolFilter. Something like this:

        HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
        filter.CacheControl.WriteBehavior = HttpCacheWriteBehavior.NoCache;
        filter.CacheControl.ReadBehavior = HttpCacheReadBehavior.MostRecent;
    
        filter.ServerCredential = new PasswordCredential(resourceUri.ToString(), username, password);
    
        HttpClient client = new HttpClient(filter);