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 Stream
is 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 a byte[]
. For example:
using(var ms = new MemoryStream()) {
response.CopyTo(ms);
byte[] payload = ms.ToArray();
}
You can store the byte[]
trivially. Then if you want a Stream
later, you can use new MemoryStream(payload)
.
Can you create a custom class that contains a byte array for storing the data and another field with HttpWebRequest.Headers for the headers? Then cache that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With