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);
}
}