Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Net.Http.HttpClient Disable Caching (.Net Standart Project)

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.

like image 712
Trax Avatar asked Nov 21 '18 09:11

Trax


2 Answers

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://docs.microsoft.com/en-us/dotnet/api/system.net.http.headers.cachecontrolheadervalue

like image 120
Agshin Nabiyev Avatar answered Sep 18 '22 12:09

Agshin Nabiyev


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);
like image 44
pvma Avatar answered Sep 19 '22 12:09

pvma