12

In my .NET Standard project I'm using System.Net.Http.HttpClient. How can I disable all caching (request caching especially) in HttpClient?

If server sends responses with no cache header problem solves. But I want to make this on client side. I want to completely disable all caching.

Thanks.

Edit: It looks like I could use WebRequestHandler but this does not exist in .NET standard. I can only use HttpClientHandler but HttpClientHandler doesn't have any option about caching.

Community
  • 1
  • 1
Trax
  • 943
  • 2
  • 12
  • 30

2 Answers2

33

You can use CacheControlHeaderValue in HttpClient

using System.Net.Http.Headers;

httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
{
  NoCache = true
}

For more information you can look https://learn.microsoft.com/en-us/dotnet/api/system.net.http.headers.cachecontrolheadervalue

marv51
  • 366
  • 1
  • 11
Agshin Nabiyev
  • 446
  • 4
  • 3
4

I tried everything, this one worked for me. Just in case some is not able to make the accepted answer work:

var uri = new Uri("http://localhost:8080/v?time=" + DateTime.Now);
var client = new HttpClient();
var response = await client.GetAsync(uri);
pvma
  • 403
  • 8
  • 14