0

I am looking for some help with a UWP application using the Windows.Web.Http.HttpClient class. I am new to UWP development so using the HttpClient is also new.

I have two calls that need to be made relatively quickly to a web service. The first call takes input from a user and posts the result to the database

Call 1:

sharedHttpResponseMessage = await sharedHttpClient.PostAsync(resourceUri, new HttpStringContent(""));

jsonObject = JsonObject.Parse(await sharedHttpResponseMessage.Content.ReadAsStringAsync());

The calls above will perform some work on a database and depending on the result the second call will pull data to display a list to the user.

Call 2:

sharedHttpResponseMessage = await sharedHttpClient.GetAsync(resourceUri);

jsonObject = JsonObject.Parse(await sharedHttpResponseMessage.Content.ReadAsStringAsync());

The issue that occurs is the second call is not returning the correct data. It is as if the first call has not completed before my second call gets the result. Is this the way HttpClient works due to the asnyc calls, or is there a way to put them together in a stack so they are called back to back? I have seen HttpRequestHandler but not sure this is the answer I need either.

Thanks for any help.

Edit 1: Here are the functions that I am calling.

// This code is called from a button press
private async void RunSetPromptTest()
{
    getPrompt = "http://internalwebaddress/set_response";

    if (!Uri.TryCreate(getPrompt, UriKind.Absolute, out resourceUri))
        return;

    sharedHttpClient.DefaultRequestHeaders.Clear();
    sharedHttpClient.DefaultRequestHeaders.Add("pn_user_session_id", "17855");
    sharedHttpClient.DefaultRequestHeaders.Add("pv_prompt_type", "set_manual_response");
    sharedHttpClient.DefaultRequestHeaders.Add("pv_value", Value.Text);

    try
    {
        sharedHttpResponseMessage = await sharedHttpClient.PostAsync(resourceUri, new HttpStringContent(""));

        jsonObject = JsonObject.Parse(await sharedHttpResponseMessage.Content.ReadAsStringAsync());
        returnStatus = jsonObject.GetNamedString("pv_status_return");

        if (returnStatus == "E")
            Prompt.Text = jsonObject.GetNamedString("pv_message_return");
        else
        {
            RunGetPromptTest();
        }


        rootPage.NotifyUser("Completed", MainPage.NotifyType.StatusMessage);

    }
    catch (Exception ex)
    {
        rootPage.NotifyUser("Error: " + ex.Message, MainPage.NotifyType.ErrorMessage);
    }
}


// This code section is called within the above procedure RunSetPromptTest.
private async void RunGetPromptTest()
{

    getPrompt = "http://internalwebaddress/get_prompt";

    if (!Uri.TryCreate(getPrompt, UriKind.Absolute, out resourceUri))
        return;

    sharedHttpClient.DefaultRequestHeaders.Clear();
    sharedHttpClient.DefaultRequestHeaders.Add("pn_user_session_id", "17855");
    sharedHttpClient.DefaultRequestHeaders.Add("pv_prompt_type", "get_manual_prompt_internal");

    try
    {
        sharedHttpResponseMessage = await sharedHttpClient.PostAsync(resourceUri, new HttpStringContent(""));

        jsonObject = JsonObject.Parse(await sharedHttpResponseMessage.Content.ReadAsStringAsync());

        Prompt.Text = jsonObject.GetNamedString("pv_message_return");
        promptList = jsonObject.GetNamedString("pv_list_return");
        Value.Text = string.Empty;

        getPrompt = "http://internalwebaddress/get_prompt_list";

        if (!Uri.TryCreate(getPrompt, UriKind.Absolute, out resourceUri))
            return;

        sharedHttpClient.DefaultRequestHeaders.Clear();
        sharedHttpClient.DefaultRequestHeaders.Add("pn_user_session_id", "17855");
        sharedHttpClient.DefaultRequestHeaders.Add("pv_prompt_type", "get_manual_list");

        sharedHttpResponseMessage = await sharedHttpClient.GetAsync(resourceUri);

        jsonObject = JsonObject.Parse(await sharedHttpResponseMessage.Content.ReadAsStringAsync());

        JsonArray jsonItem = jsonObject.GetNamedArray("items", new JsonArray());

        rootPage.NotifyUser("Completed", MainPage.NotifyType.StatusMessage);

    }
    catch (Exception ex)
    {
        rootPage.NotifyUser("Error: " + ex.Message, MainPage.NotifyType.ErrorMessage);
    }
}
Josh S.
  • 1
  • 1
  • In short, it should work and the problem is hidden in details which you have not shared. What does it mean "it does not return correct data". Do you have proof? How the entire method that does the two calls looks like? – Liero Mar 08 '17 at 17:25
  • Without seeing the two calls in context of one another its hard to say what your problem is – maccettura Mar 08 '17 at 17:28
  • I have added the code I am calling. I updated it a bit to better reflect what I am seeing. – Josh S. Mar 08 '17 at 18:34
  • Have you confirm the first call response successful,if not the second call will get error response. You have used await a few times to ensure that the two requests are executed synchronously – Nico Zhu Mar 09 '17 at 06:08

1 Answers1

0

Found the resolution on this thread from 2014 after quite a bit of searching. I had the same issue, but was not aware that it was the same issue at the time of my post.

HTTPClient every time returns the same string

Creating this fixed the issue with cached data.

Windows.Web.Http.Filters.HttpBaseProtocolFilter filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
filter.CacheControl.ReadBehavior = Windows.Web.Http.Filters.HttpCacheReadBehavior.MostRecent;
Community
  • 1
  • 1
Josh S.
  • 1
  • 1