今天在用.NET利用IHttpModel实现网站静态缓存的时候,不知道最后为什么用 Server.Transfer(html)的时候结果输出的是HTML的源代码。

贴上源代码

using System;
using System.Web;
using System.Text.RegularExpressions;
using System.IO;
using System.Configuration;
using System.Collections.Generic;
namespace Product
{
public class ProductModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));//请求开始
application.EndRequest += (new EventHandler(this.Application_EndRequest));//请求结束
}
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication Application = (HttpApplication)source;
CheckUrl(Application);
}
private void Application_EndRequest(Object source, EventArgs e)
{
//HttpApplication Application = (HttpApplication)source;
//Application.Response.Write("test");
}
private void CheckUrl(HttpApplication application)
{
if (application.Request.RequestType.ToUpper() == "POST" || application.Request.UserAgent.ToLower() == "product")
{
return;
} string[] resUrlTemp = new string[];//待缓存的请求模板
resUrlTemp[] = "/model/modelsearch.aspx\\?clid=([\\d]+)&coid=([\\d]+)&bid=([\\d]+)&price=(.*)&model=(.*)&p=([\\d]+)&t=([0-2]{1,1})";
resUrlTemp[] = "/pic/imgsearch.aspx\\?clid=([\\d]+)&cbid=([\\d]+)&model=(.*)&p=([\\d]+)";
resUrlTemp[] = "/praise/PraiseSearch.aspx\\?clid=([\\d]+)&coid=([\\d]+)&model=(.*)&p=([\\d]+)";
resUrlTemp[] = "/price/sellerpricesearch.aspx\\?clid=([\\d]+)&coid=([\\d]+)&brand=([\\d]+)&price=(.*)&model=(.*)&p=([\\d]+)&t=([0-2]{1,1})";
resUrlTemp[] = "/dealer/sellersearch.aspx\\?pve=([\\d]+)&city=([\\d]+)&type=([\\d]+)&seller=(.*)&bid=([\\d]+)&model=(.*)&pagesize=([\\d]+)&p=([\\d]+)"; string reqUrl = application.Context.Request.Url.PathAndQuery.ToLower();//请求动态路径 bool success = false;
for (int i = ; i < resUrlTemp.Length;i++ )
{
if (!success)
{
Regex reg = new Regex(resUrlTemp[i]);//匹配当前请求是否需要缓存
MatchCollection mc = reg.Matches(reqUrl);
if (mc.Count > )
{
//静态页命名使用当前请求路径MD5加密命名
string PyReUrl = ConfigurationManager.ConnectionStrings["WebPhysicsUrl"].ConnectionString + "/Cache/" + i + "/" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(reqUrl, "MD5") + ".html"; FileInfo fi = new FileInfo(PyReUrl);//判断是否缓存
if (!fi.Exists)
{
//缓存页面
string WebUrl = ConfigurationManager.ConnectionStrings["WebUrl"].ConnectionString;
System.Net.HttpWebRequest Request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(WebUrl + reqUrl);//Post请求当前页
Request.Method = "GET";
Request.Accept = "*/*";
Request.UserAgent = "Product";
Request.AllowAutoRedirect = true;
Request.MaximumAutomaticRedirections = ; System.Net.HttpWebResponse Response = null;
try
{
Response = (System.Net.HttpWebResponse)Request.GetResponse();//获得响应
}
catch(Exception ex)
{
application.Response.Write("Response Error"+ex.Message);
}
if (Response != null)
{
System.IO.Stream strm = Response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(strm, System.Text.Encoding.GetEncoding("gb2312")); StreamWriter Sw = null;
try
{
if (!Directory.Exists(Directory.GetParent(PyReUrl).FullName))
Directory.CreateDirectory(Directory.GetParent(PyReUrl).FullName); FileStream Fs = new FileStream(PyReUrl, FileMode.Create, FileAccess.Write, FileShare.Read);
Sw = new StreamWriter(Fs, System.Text.Encoding.Default, ); Sw.Write(sr.ReadToEnd());//写入 success = true;
}
catch(Exception ex)
{
Sw.Close();
application.Response.Write("Writer Error"+ex.Message);
} sr.Close();
Sw.Close();
Response.Close();
}
}
else
{
//application.Response.Redirect(PyReUrl);//链接到静态页面 浏览器请求路径不变,不会影响收录
application.Server.Transfer("/Cache/" + i + "/" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(reqUrl, "MD5") + ".html");
}
}
}
} }
public void Dispose()
{
}
}
}

我的问题是这样的,我的网站采用的UTF8编码

当这两个流采用gb2312的时候,Service.Transfer是可以正常显示HTMl的,但是页面乱码

  System.IO.Stream strm = Response.GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(strm, System.Text.Encoding.GetEncoding("gb2312"));
 FileStream Fs = new FileStream(PyReUrl, FileMode.Create, FileAccess.Write, FileShare.Read);
Sw = new StreamWriter(Fs, System.Text.Encoding.Default, );

但是当我采用UTF编码的时候,页面缓存没问题,但是Service.Transfer就只是显示源代码,不知道为什么。求解。

此贴用于记录,如果找到解决方案后,我会贴出来!

个人解决方案:

个人觉得很误解,问了好多人,都没有遇到过这种情况,不知道是不是只有我一个人遇到过。。。。。。。

说下个人的解决办法把。

上代码

application.Response.Write(File.ReadAllText(application.Server.MapPath("/qq534550354/" + i + "/" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(reqUrl, "MD5") + ".html")));
application.Response.End();

我首先读取缓存文件的内容,然后用write去写,暂时就这样吧。以后如果有好的解决方案了,在做修改!

利用Ihttpmodel实现网站缓存,解决Server.Transfer 直接输出HTML源代码的问题的更多相关文章

  1. Response.Redirect(),Server.Transfer(),Server.Execute()的区别与网站优化

    转 http://blog.csdn.net/dannywj1371/article/details/10213631 1.Response.Redirect():Response.Redirect方 ...

  2. 利用IIS部署WEB网站以及解决CSS/JS不能显示问题

    利用IIS部署WEB网站以及解决CSS/JS不能显示问题 转载声明:http://blog.sina.com.cn/s/blog_a001e5980101b4kt.html vs中正常IIS发布网站后 ...

  3. 页面跳转 Server.Transfer和 Response.Redirect的区别

    1.Server.Transfer 用于把处理的控制权从一个页面转移到另一个页面,在转移的工程中没有离开服务器内部控件(如request,session等)保存的信息不变.因此你能从a页面跳转到b页面 ...

  4. Server.Transfer 和 Response.Redirect 用法区别

    在ASP.NET中,在后台传值方式目前大多都是用 Response.Redirect("页面地址") 来重定向页面的,但是现在还有一种方式也可以达到重定向页面的作用,而且在某些时刻 ...

  5. URL、Session、Cookies、Server.Transfer、Application和跨页面传送,利弊比较

    URL.Session.Cookies.Server.Transfer.Application和跨页面传送.-本题考查面试者对ASP.NET中多页面传值的理解是否全面.因为ASP.NET的页面表单提交 ...

  6. Response.Redirect()、Server.Execute和Server.Transfer的区别

    1.Response.Redirect(): Response.Redirect方法导致浏览器链接到一个指定的URL. 当Response.Redirect()方法被调用时,它会创建一个应答,应答头中 ...

  7. Server.Transfer,Response.Redirect用法点睛

    Server.Transfer,Response.Redirect的区别 如果你读过很多行业杂志和 ASP.NET 示例,你会发现,大多数人使用 Response.Redirect 将用户引导到另一个 ...

  8. Response.Redirect与Server.Transfer区别-转

    执行过程: 1.浏览器ASP文件请求->服务器执行->遇到response.redirect语句->服务器发送response.redirect后面的地址给客户机端的浏览器-> ...

  9. SpringBoot30 整合Mybatis-Plus、整合Redis、利用Ehcache实现二级缓存、利用SpringCache和Redis作为缓存

    1 环境说明 JDK: 1.8 MAVEN: 3. SpringBoot: 2.0.4 2 SpringBoot集成Mybatis-Plus 2.1 创建SpringBoot 利用IDEA创建Spri ...

随机推荐

  1. VS2008非托管C++调用wcf(WebService)服务

    在Visual Studio 2008以及以后版本中,微软停止了非托管C++的直接WebService引用.不过ATL Server代码已经托管到开源网站上,我们可以找到ATL Server的源代码, ...

  2. 使用msys

    下载地址:http://msys2.github.io/ 更新:pacman -Syu 安装git:pacman -S git 或者使用cygwin 调色:编辑~/.minttyrc Foregrou ...

  3. Java class文件分析工具 -- Classpy

    Classpy Classpy是一个图形化的class文件分析工具,功能和javap类似,界面主要參考了Java Class Viewer: 为什么要又一次创造轮子? 写这个工具花了将近一周的时间.那 ...

  4. 常用的shell命令整理

    工作快一年了,shell命令也玩了一年了.还是有点积累的,下面是本人常用的. 1.pwd | xargs -i basename {}   获取当前所在目录的名称 2.ps -ef|grep -w   ...

  5. 巡逻机器人(BFS)

    巡逻机器人问题(F - BFS,推荐) Description   A robot has to patrol around a rectangular area which is in a form ...

  6. C语言--union关键字(转载)

    union维护足够的空间来放置多个数据成员中的“一种”,而不是为每一个数据成员配置空间.在union中,所有的数据成员共用一个空间,同一时间只能存储其中一个数据成员,所有的数据成员具有相同的起始地址.

  7. c# 数据库编程(利用DataSet 和 DataAdaper对象操作数据库--单表操作)

    一.概述 前面2篇文章,介绍了使用SqlCommand对象利用sql命令来操作数据库. 这篇文章我们来介绍使用c#的DataSet 和 DataAdaper对象操作操作数据库. 先来介绍下这两个对象是 ...

  8. 从java main方法说开去(转)

    刚刚接触java语言时,接触的便为一个java main方法.我们知道这样程序就可以运行了,但是程序是怎么运行起来的我们却不知道. 众所周知,当执行一个java程序时,首先会启动一个JVM虚拟机进程, ...

  9. 说服式设计(persuasive design)的行为模型

    转自:http://www.sharetk.com/html/ued/User-Research/1404.html 一 模型简介 BJ Fogg提出了一个新的理解人类行为的模型,他称之为Fogg b ...

  10. C# 点击窗口任意位置拖动

    代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...