了HTTPClient每次都返回相同的字符串 [英] HTTPClient every time returns the same string
本文介绍了了HTTPClient每次都返回相同的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
能有一个人让我明白为什么我的代码每次都返回相同的字符串?
Could some one make me clear why my code returns the same string every time?
public MainPage()
{
this.InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(5);
timer.Tick += OnTimerTick;
timer.Start();
}
private void OnTimerTick(object sender, object e)
{
getData();
HubText.Text = dumpstr;
}
private async void getData()
{
// Create an HttpClient instance
HttpClient client = new HttpClient();
var uri = new Uri("http://192.168.4.160:8081/v");
try
{
// Send a request asynchronously continue when complete
HttpResponseMessage response = await client.GetAsync(uri);
// Check that response was successful or throw exception
response.EnsureSuccessStatusCode();
// Read response asynchronously
dumpstr = await response.Content.ReadAsStringAsync();
}
catch (Exception e)
{
//throw;
}
}
string dumpstr;
所以,每隔5秒钟,我得到了我在第一个请求得到了相同的字符串。
我在做什么错了?
So every 5 seconds I get the same string that I got in my first request. What am I doing wrong?
推荐答案
这是因为你在做一个GET相同的URL。根据HTTP语义,该值应该是一个合理的时间内是相同的,所以操作系统缓存为您的响应。
It's because you're doing a GET to the same URL. According to the HTTP semantics, the value should be the same within a reasonable timeframe, so the OS is caching the response for you.
您可以通过任何绕过缓存这些方法:
You can bypass the cache by any of these methods:
- 使用POST请求
- 添加查询字符串参数是不同的。每个呼叫。
- 指定(在服务器上)禁止或限制缓存响应头允许的。
这篇关于了HTTPClient每次都返回相同的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文