private Stream WriteResponseToCache(Stream responseStream) { StringBuilder fileNameBuffer = new StringBuilder(Kernel32.MAX_PATH * 2); bool created = WinInet.CreateUrlCacheEntry( _requestUrl, 0, UrlHelper.GetExtensionForUrl(_requestUrl), fileNameBuffer, 0); if (created) { // copy the stream to the file string cacheFileName = fileNameBuffer.ToString(); using (FileStream cacheFile = new FileStream(cacheFileName, FileMode.Create)) StreamHelper.Transfer(responseStream, cacheFile); // commit the file to the cache System.Runtime.InteropServices.ComTypes.FILETIME zeroFiletime = new System.Runtime.InteropServices.ComTypes.FILETIME(); bool committed = WinInet.CommitUrlCacheEntry( _requestUrl, cacheFileName, zeroFiletime, zeroFiletime, CACHE_ENTRY.NORMAL, IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero); Trace.Assert(committed); // return a stream to the file return(new FileStream(cacheFileName, FileMode.Open)); } else { Trace.Fail("Unexpedcted failure to create cache entry for url " + _requestUrl + ": " + Marshal.GetLastWin32Error().ToString(CultureInfo.InvariantCulture)); return(responseStream); } }