I’d like to cache the response I get from a HttpWebRequest. I need both the ResponseStream and the headers. Using HttpRequestCachePolicy(HttpCacheAgeControl.MaxAge, TimeSpan.FromDays(1)) when creating the request doesn’t seem to work (IsFromCache is never true for some reason), and I’m a bit scared of manually caching the entire HttpWebResponse since it’s containing a stream of gzipped data, and I have a bad feeling about storing streams in the ASP.NET cache.
The response is mapped to an object like this (simplified):
public readonly Stream Response;
public readonly string Etag;
private MyObject(Stream response, string etag)
{
this.Response = response;
this.Etag = etag;
}
Since the object also contains the response stream I face the same issue here.
How do I cache this?
A
Streamis a pipe, not a bucket. If you need to store it, you must first get the actual contents. For example, you could read it all into abyte[]. For example:You can store the
byte[]trivially. Then if you want aStreamlater, you can usenew MemoryStream(payload).