예제 #1
0
        public override bool OnAfterSessionComplete(Session oSession)
        {
            if (settings.CacheEnabled && oSession.responseCode == 200)
            {
                string filepath = TaskRecord.GetAndRemove(oSession.fullUrl);
                if (!(string.IsNullOrEmpty(filepath) || CheckedFiles.Contains(filepath)))
                {
                    if (File.Exists(filepath))
                    {
                        File.Delete(filepath);
                    }

                    //保存下载文件并记录Modified-Time
                    try
                    {
                        if (settings.ShowCacheLog)
                        {
                            ElectronicObserver.Utility.Logger.Add(2, string.Format("更新缓存文件: {0}.", filepath));
                        }

                        oSession.SaveResponseBody(filepath);
                        _SaveModifiedTime(filepath, oSession.oResponse.headers["Last-Modified"]);
                        //Debug.WriteLine("CACHR> 【下载文件】" + oSession.PathAndQuery);
                    }
                    catch (Exception ex)
                    {
                        ElectronicObserver.Utility.ErrorReporter.SendErrorReport(ex, "会话结束时,保存返回文件时发生异常:" + oSession.fullUrl);
                    }
                }

                return(true);
            }

            return(false);
        }
예제 #2
0
        public override bool OnBeforeResponse(Session oSession)
        {
            MakaiOnBeforeResponse(oSession);

            if (settings.CacheEnabled && oSession.PathAndQuery.StartsWith("/kcs/"))
            {
                if (oSession.responseCode == 304)
                {
                    string filepath = TaskRecord.GetAndRemove(oSession.fullUrl);
                    //只有TaskRecord中有记录的文件才是验证的文件,才需要修改Header
                    if (!string.IsNullOrEmpty(filepath))
                    {
                        CheckedFiles.Add(filepath);

                        //服务器返回304,文件没有修改 -> 返回本地文件
                        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";
                        }
                    }

                    return(true);
                }
                else if (oSession.responseCode == 200)
                {
                    // 由服务器下载所得
                    if (oSession.PathAndQuery.StartsWith("/kcs/sound") && oSession.PathAndQuery.IndexOf("titlecall/") < 0)
                    {
                        oSession.oResponse.headers["Pragma"]        = "no-cache";
                        oSession.oResponse.headers["Cache-Control"] = "no-cache";
                    }
                    return(true);
                }
            }

            return(false);
        }