[英]C# WP8: How to call an async task method consecutively and get a new response every time?
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. 我试图每次在页面上按下按钮时都进行一次API调用。 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.
但是,现在每次按下按钮时,我都会从“ GetjsonStream”方法获得相同的响应。
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.
我仍然对C#还是陌生的,并且在不断学习中,因此我希望我缺少一些明显的东西。
EDIT: Some clarification: 编辑:一些澄清:
The URL calls a random generator. URL调用随机生成器。 So it should be a different result every time.
因此,每次都会得到不同的结果。 When stepping through the "GetjsonStream" method, "content" will be the same every time.
当逐步执行“ GetjsonStream”方法时,“内容”将始终相同。
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.
我想知道是否有一些超时设置。
I am pretty sure I am mis-handling the async method.
我很确定我对异步方法的处理不正确。
No, but you are mishandling HTTP. 否,但是您处理HTTP错误。 :)
:)
HTTP gets can be cached. 可以缓存HTTP获取。 Since the API should always return a different result, the server should be modified to send headers disabling caching.
由于API应始终返回不同的结果,因此应修改服务器以发送标头以禁用缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.