Skip to content

Instantly share code, notes, and snippets.

@aarondandy

aarondandy/code Secret

Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarondandy/ee11865494c4b1ea5004 to your computer and use it in GitHub Desktop.
Save aarondandy/ee11865494c4b1ea5004 to your computer and use it in GitHub Desktop.
Cache loses content type.
using System;
using System.Net;
using System.Net.Cache;
using System.Net.Http;
using System.Threading.Tasks;
namespace HttpCacheTest
{
class Program
{
const string url = "http://<redacted>/octopus/api/projects/projects-<redacted>";
const string apiKey = "<redacted>";
static void Main(string[] args)
{
DefaultTest().Wait();
Task.Delay(1000).Wait();
CachingTest().Wait();
}
static async Task DefaultTest()
{
Console.WriteLine("Default:");
using (var httpClient = new HttpClient()
{
DefaultRequestHeaders = {
{ "X-Octopus-ApiKey", apiKey}
}
})
{
using (var response = await httpClient.GetAsync(url))
{
Console.WriteLine($"{response.Content.Headers.ContentType}");
}
await Task.Delay(1000);
using (var response = await httpClient.GetAsync(url))
{
Console.WriteLine($"{response.Content.Headers.ContentType}");
}
}
}
static async Task CachingTest()
{
Console.WriteLine("With Cache:");
using (var httpClient = new HttpClient(new WebRequestHandler()
{
CachePolicy = new RequestCachePolicy(RequestCacheLevel.Default)
})
{
DefaultRequestHeaders = {
{ "X-Octopus-ApiKey", apiKey}
}
})
{
using (var response = await httpClient.GetAsync(url))
{
Console.WriteLine($"{response.Content.Headers.ContentType}");
}
await Task.Delay(1000);
using (var response = await httpClient.GetAsync(url))
{
Console.WriteLine($"{response.Content.Headers.ContentType}");
}
}
}
}
}
Default:
application/json; charset=utf-8
application/json; charset=utf-8
With Cache:
application/json; charset=utf-8
text/plain
Press any key to continue . . .
# Result Protocol Host URL Body Caching Content-Type Process Comments Custom
1 200 HTTP <redacted> /octopus/api/projects/<redacted> 1,379 must-revalidate; Expires: Tue, 05 May 2015 03:18:10 GMT application/json; charset=utf-8 httpcachetest.vshost:3228
2 200 HTTP <redacted> /octopus/api/projects/<redacted> 1,379 must-revalidate; Expires: Tue, 05 May 2015 03:18:11 GMT application/json; charset=utf-8 httpcachetest.vshost:3228
3 200 HTTP <redacted> /octopus/api/projects/<redacted> 1,379 must-revalidate; Expires: Tue, 05 May 2015 03:18:13 GMT application/json; charset=utf-8 httpcachetest.vshost:3228
4 304 HTTP <redacted> /octopus/api/projects/<redacted> 0 no-cache; Expires: Tue, 05 May 2015 03:18:14 GMT text/plain httpcachetest.vshost:3228
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment