public ExtendedHttpWebResponse(Uri responseUri, WebResponse response, Stream responseStream, CacheInfo cacheInfo) { this.responseUri = responseUri; this.response = response; this.responseStream = responseStream; this.cacheInfo = cacheInfo; }
private WebRequest getRequest(CacheInfo cacheInfo) { WebRequest request; if(cacheInfo != null && cacheInfo.CachedUri != null && cacheInfo.Expires > DateTime.Now) { request = WebRequest.Create(cacheInfo.CachedUri); } else { request = WebRequest.CreateDefault(RequestUri); } HttpWebRequest hRequest = request as HttpWebRequest; if(hRequest != null && cacheInfo != null && cacheInfo.CachedUri != null) { if(cacheInfo.ETag != null) { hRequest.Headers["If-None-Match"] = cacheInfo.ETag; } if(cacheInfo.LastModified != DateTime.MinValue) { hRequest.IfModifiedSince = cacheInfo.LastModified; } hRequest.Headers["Accept-Encoding"] = "deflate, gzip" ; } return request; }
private CacheInfo processResponse(WebResponse response) { HttpWebResponse hResponse = response as HttpWebResponse; CacheInfo cacheInfo = null; if(hResponse != null) { DateTime expires; if(hResponse.Headers["Expires"] != null) { expires = DateTime.Parse(hResponse.Headers["Expires"]); } else { expires = DateTime.MinValue; } cacheInfo = new CacheInfo(expires, hResponse.Headers["Etag"], hResponse.LastModified, null, hResponse.ContentType); } return cacheInfo; }
private WebResponse getResponse(WebRequest request, CacheInfo cacheInfo) { WebResponse response = null; try { response = request.GetResponse(); } catch(WebException webEx) { HttpWebResponse hresp2 = webEx.Response as HttpWebResponse; if(hresp2 != null) { if(hresp2.StatusCode == HttpStatusCode.NotModified) { if(cacheInfo != null && cacheInfo.CachedUri != null) { request = WebRequest.Create(cacheInfo.CachedUri); } else { request = WebRequest.Create(RequestUri); } response = request.GetResponse(); } } } return response; }
public void SetCacheInfo(Uri uri, CacheInfo cacheInfo, Stream stream) { }
public void SetCacheInfo(Uri uri, CacheInfo cacheInfo, Stream stream) { XmlElement cacheElm = GetCacheElm(uri); if(cacheInfo != null) { if(cacheInfo.ETag != null) { cacheElm.SetAttribute("etag", cacheInfo.ETag); } else { cacheElm.RemoveAttribute("etag"); } if(cacheInfo.ContentType != null) { cacheElm.SetAttribute("content-type", cacheInfo.ContentType); } else { cacheElm.RemoveAttribute("content-type"); } if(cacheInfo.Expires != DateTime.MinValue) { cacheElm.SetAttribute("expires", cacheInfo.Expires.ToString("s")); } else { cacheElm.RemoveAttribute("expires"); } if(cacheInfo.LastModified != DateTime.MinValue) { cacheElm.SetAttribute("last-modified", cacheInfo.LastModified.ToString("s")); } else { cacheElm.RemoveAttribute("last-modified"); } } if (stream != null) { string localPath; if(cacheElm.HasAttribute("local-path")) { localPath = cacheElm.GetAttribute("local-path"); } else { localPath = Guid.NewGuid().ToString() + ".cache"; cacheElm.SetAttribute("local-path", localPath); } stream.Position = 0; int count; byte[] buffer = new byte[4096]; FileStream fs = File.OpenWrite(Path.Combine(cacheDir, localPath)); while((count = stream.Read(buffer, 0, 4096)) > 0) fs.Write(buffer, 0, count); fs.Flush(); fs.Close(); } SaveDoc(); }
/// <summary> /// Initializes an instance of the <see cref="ExtendedHttpWebResponse"/> class with the specified parameters. /// </summary> /// <param name="responseUri">The URI of the Internet resource that actually responded to the request.</param> /// <param name="response">The wrapped <see cref="WebResponse"/> instance, that provides a response from a Uniform Resource Identifier (URI). </param> /// <param name="responseStream">The data stream from the Internet resource.</param> /// <param name="cacheInfo">The caching information.</param> public ExtendedHttpWebResponse(Uri responseUri, WebResponse response, Stream responseStream, CacheInfo cacheInfo) { _responseUri = responseUri; _response = response; _responseStream = responseStream; _cacheInfo = cacheInfo; }