Reputation: 35
This method:
public async Task<string> GetjsonStream(string url)
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url);
string content = await response.Content.ReadAsStringAsync();
return content;
}
Is called by this method:
private async void button_Click(object sender, RoutedEventArgs e)
{
String URL = URL_core + API_call;
try
{
string response = await GetjsonStream(URL);
JSONReturn answere = JsonConvert.DeserializeObject<JSONReturn>(response);
output_box.Text = answere.value;
}
catch (Exception ex)
{
output_box.Text = URL;
}
}
I am trying to have an API call happen every time a button is pressed on a page. I want to be able to keep pressing the button without re-loading the page and get a new result. However, right now I get the same response from the "GetjsonStream" method every time I press the button.
I am pretty sure I am mis-handling the async method. It seems like I need to either force a wait for it or initiate a fresh instance of the task. I am still new to C# and am learning as I go along, so I'm hoping there is just something obvious that I am missing.
EDIT: Some clarification:
The URL calls a random generator. So it should be a different result every time. When stepping through the "GetjsonStream" method, "content" will be the same every time.
Also, I found that if I wait for a minute or two, and press the button again, I do get a new result. I'm wondering if there is some timeout setting in place.
Upvotes: 1
Views: 810
Reputation: 456647
I am pretty sure I am mis-handling the async method.
No, but you are mishandling HTTP. :)
HTTP gets can be cached. Since the API should always return a different result, the server should be modified to send headers disabling caching.
Upvotes: 1