static void _AfterComplete(Session oSession) { if (!Settings.Current.CacheEnabled) { return; } if (!_Filter(oSession)) { return; } //服务器返回200,下载新的文件 if (oSession.responseCode == 200) { string filepath = TaskRecord.GetAndRemove(oSession.fullUrl); //只有TaskRecord中有记录的文件才是验证的文件,才需要保存 if (!string.IsNullOrEmpty(filepath)) { if (File.Exists(filepath)) { File.Delete(filepath); } //保存下载文件并记录Modified-Time try { oSession.SaveResponseBody(filepath); //cache.RecordNewModifiedTime(oSession.fullUrl, // oSession.oResponse.headers["Last-Modified"]); _SaveModifiedTime(filepath, oSession.oResponse.headers["Last-Modified"]); //Debug.WriteLine("CACHR> 【下载文件】" + oSession.PathAndQuery); } catch (Exception ex) { Log.Exception(oSession, ex, "会话结束时,保存返回文件时发生异常"); } } } }
static void _AfterComplete(Session oSession) { if (!Settings.Current.CacheEnabled) { return; } if (!_Filter(oSession)) { return; } //Download new files following a HTTP 200 response if (oSession.responseCode == 200) { string filepath = TaskRecord.GetAndRemove(oSession.fullUrl); //Only write files mentioned in TaskRecord to disk if (!string.IsNullOrEmpty(filepath)) { if (File.Exists(filepath)) { File.Delete(filepath); } //Save file in response and write "Last-Modified" Metadata try { oSession.SaveResponseBody(filepath); //cache.RecordNewModifiedTime(oSession.fullUrl, // oSession.oResponse.headers["Last-Modified"]); _SaveModifiedTime(filepath, oSession.oResponse.headers["Last-Modified"]); //Debug.WriteLine("CACHR> 【FileDownloaded】" + oSession.PathAndQuery); } catch (Exception ex) { Log.Exception(oSession, ex, "An error occured while writing downloaded file to disk"); } } } }
//This event fires when a server response is received by Fiddler static void _BeforeResponse(Session oSession) { if (!_Filter(oSession)) { return; } if (oSession.responseCode == 304) { string filepath = TaskRecord.GetAndRemove(oSession.fullUrl); //只有TaskRecord中有记录的文件才是验证的文件,才需要修改Header if (!string.IsNullOrEmpty(filepath)) { //服务器返回304,文件没有修改 -> 返回本地文件 oSession.bBufferResponse = true; byte[] file; using (var fs = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read)) { file = new byte[fs.Length]; fs.Read(file, 0, (int)fs.Length); } oSession.ResponseBody = file; oSession.oResponse.headers.HTTPResponseCode = 200; oSession.oResponse.headers.HTTPResponseStatus = "200 OK"; oSession.oResponse.headers["Last-Modified"] = oSession.oRequest.headers["If-Modified-Since"]; oSession.oResponse.headers["Accept-Ranges"] = "bytes"; oSession.oResponse.headers.Remove("If-Modified-Since"); oSession.oRequest.headers.Remove("If-Modified-Since"); if (filepath.EndsWith(".swf")) { oSession.oResponse.headers["Content-Type"] = "application/x-shockwave-flash"; } //Debug.WriteLine(oSession.oResponse.headers.ToString()); //Debug.WriteLine(""); //Debug.WriteLine("CACHR> 【捕获 304】" + oSession.PathAndQuery); //Debug.WriteLine(" " + filepath); } } }
//This event fires when a server response is received by Fiddler static void _BeforeResponse(Session oSession) { if (!_Filter(oSession)) { return; } if (oSession.responseCode == 304) { string filepath = TaskRecord.GetAndRemove(oSession.fullUrl); //Only modify headers for those files mentioned in TaskRecord if (!string.IsNullOrEmpty(filepath)) { //If response HTTP 304 is received, then the local file is up to date and can be used oSession.bBufferResponse = true; byte[] file; using (var fs = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.Read)) { file = new byte[fs.Length]; fs.Read(file, 0, (int)fs.Length); } oSession.ResponseBody = file; oSession.oResponse.headers.HTTPResponseCode = 200; oSession.oResponse.headers.HTTPResponseStatus = "200 OK"; oSession.oResponse.headers["Last-Modified"] = oSession.oRequest.headers["If-Modified-Since"]; oSession.oResponse.headers["Accept-Ranges"] = "bytes"; oSession.oResponse.headers.Remove("If-Modified-Since"); oSession.oRequest.headers.Remove("If-Modified-Since"); if (filepath.EndsWith(".swf")) { oSession.oResponse.headers["Content-Type"] = "application/x-shockwave-flash"; } //Debug.WriteLine(oSession.oResponse.headers.ToString()); //Debug.WriteLine(""); //Debug.WriteLine("CACHR> 【HTTP 304】" + oSession.PathAndQuery); //Debug.WriteLine(" " + filepath); } } }
void _RecordTask(string url, string filepath) { TaskRecord.Add(url, filepath); }