wpf HTTPClient 每次都返回相同的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21643747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTPClient every time returns the same string
提问by SABlyu
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;
So every 5 seconds I get the same string that I got in my first request. What am I doing wrong?
所以每 5 秒我就会得到与我在第一个请求中得到的相同的字符串。我究竟做错了什么?
回答by kiewic
If you are using Windows.Web.Http.HttpClient, you can skip the local cache this way:
如果您正在使用Windows.Web.Http.HttpClient,您可以通过这种方式跳过本地缓存:
Windows.Web.Http.Filters.HttpBaseProtocolFilter filter =
new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
filter.CacheControl.ReadBehavior =
Windows.Web.Http.Filters.HttpCacheReadBehavior.MostRecent;
HttpClient client = new HttpClient(filter);
Uri uri = new Uri("http://example.com");
HttpResponseMessage response = await client.GetAsync(uri);
response.EnsureSuccessStatusCode();
string str = await response.Content.ReadAsStringAsync();
You will never get the same response twice again :)
你再也不会得到同样的回应了 :)
But if you have access to the server source code, the most elegant fix would be to disable the cache for the URI you are downloading, i.e., add the Cache-Control: no-cacheheader.
但是,如果您可以访问服务器源代码,最优雅的解决方法是禁用正在下载的 URI 的缓存,即添加Cache-Control: no-cache标头。
回答by Stephen Cleary
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.
这是因为您正在对相同的 URL 执行 GET。根据 HTTP 语义,该值应该在合理的时间范围内相同,因此操作系统会为您缓存响应。
You can bypass the cache by any of these methods:
您可以通过以下任何一种方法绕过缓存:
- Using a POST request.
- Adding a query string parameter that is different for each call.
- Specifying (on the server) response headers that disable or limit the caching allowed.
- 使用 POST 请求。
- 添加每个调用不同的查询字符串参数。
- 指定(在服务器上)禁用或限制缓存的响应头。
回答by pvma
I tried everything, this one worked for me. Just in case some is not able to make it work:
我尝试了一切,这个对我有用。以防万一有些人无法使其工作:
var uri = new Uri("http://192.168.4.160:8081/v?time=" + DateTime.Now);
var uri = new Uri("http://192.168.4.160:8081/v?time=" + DateTime.Now);

