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

贴上源代码

  1. using System;
  2. using System.Web;
  3. using System.Text.RegularExpressions;
  4. using System.IO;
  5. using System.Configuration;
  6. using System.Collections.Generic;
  7. namespace Product
  8. {
  9. public class ProductModule : IHttpModule
  10. {
  11. public void Init(HttpApplication application)
  12. {
  13. application.BeginRequest += (new EventHandler(this.Application_BeginRequest));//请求开始
  14. application.EndRequest += (new EventHandler(this.Application_EndRequest));//请求结束
  15. }
  16. private void Application_BeginRequest(Object source, EventArgs e)
  17. {
  18. HttpApplication Application = (HttpApplication)source;
  19. CheckUrl(Application);
  20. }
  21. private void Application_EndRequest(Object source, EventArgs e)
  22. {
  23. //HttpApplication Application = (HttpApplication)source;
  24. //Application.Response.Write("test");
  25. }
  26. private void CheckUrl(HttpApplication application)
  27. {
  28. if (application.Request.RequestType.ToUpper() == "POST" || application.Request.UserAgent.ToLower() == "product")
  29. {
  30. return;
  31. }
  32.  
  33. string[] resUrlTemp = new string[];//待缓存的请求模板
  34. resUrlTemp[] = "/model/modelsearch.aspx\\?clid=([\\d]+)&coid=([\\d]+)&bid=([\\d]+)&price=(.*)&model=(.*)&p=([\\d]+)&t=([0-2]{1,1})";
  35. resUrlTemp[] = "/pic/imgsearch.aspx\\?clid=([\\d]+)&cbid=([\\d]+)&model=(.*)&p=([\\d]+)";
  36. resUrlTemp[] = "/praise/PraiseSearch.aspx\\?clid=([\\d]+)&coid=([\\d]+)&model=(.*)&p=([\\d]+)";
  37. resUrlTemp[] = "/price/sellerpricesearch.aspx\\?clid=([\\d]+)&coid=([\\d]+)&brand=([\\d]+)&price=(.*)&model=(.*)&p=([\\d]+)&t=([0-2]{1,1})";
  38. resUrlTemp[] = "/dealer/sellersearch.aspx\\?pve=([\\d]+)&city=([\\d]+)&type=([\\d]+)&seller=(.*)&bid=([\\d]+)&model=(.*)&pagesize=([\\d]+)&p=([\\d]+)";
  39.  
  40. string reqUrl = application.Context.Request.Url.PathAndQuery.ToLower();//请求动态路径
  41.  
  42. bool success = false;
  43. for (int i = ; i < resUrlTemp.Length;i++ )
  44. {
  45. if (!success)
  46. {
  47. Regex reg = new Regex(resUrlTemp[i]);//匹配当前请求是否需要缓存
  48. MatchCollection mc = reg.Matches(reqUrl);
  49. if (mc.Count > )
  50. {
  51. //静态页命名使用当前请求路径MD5加密命名
  52. string PyReUrl = ConfigurationManager.ConnectionStrings["WebPhysicsUrl"].ConnectionString + "/Cache/" + i + "/" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(reqUrl, "MD5") + ".html";
  53.  
  54. FileInfo fi = new FileInfo(PyReUrl);//判断是否缓存
  55. if (!fi.Exists)
  56. {
  57. //缓存页面
  58. string WebUrl = ConfigurationManager.ConnectionStrings["WebUrl"].ConnectionString;
  59. System.Net.HttpWebRequest Request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(WebUrl + reqUrl);//Post请求当前页
  60. Request.Method = "GET";
  61. Request.Accept = "*/*";
  62. Request.UserAgent = "Product";
  63. Request.AllowAutoRedirect = true;
  64. Request.MaximumAutomaticRedirections = ;
  65.  
  66. System.Net.HttpWebResponse Response = null;
  67. try
  68. {
  69. Response = (System.Net.HttpWebResponse)Request.GetResponse();//获得响应
  70. }
  71. catch(Exception ex)
  72. {
  73. application.Response.Write("Response Error"+ex.Message);
  74. }
  75. if (Response != null)
  76. {
  77. System.IO.Stream strm = Response.GetResponseStream();
  78. System.IO.StreamReader sr = new System.IO.StreamReader(strm, System.Text.Encoding.GetEncoding("gb2312"));
  79.  
  80. StreamWriter Sw = null;
  81. try
  82. {
  83. if (!Directory.Exists(Directory.GetParent(PyReUrl).FullName))
  84. Directory.CreateDirectory(Directory.GetParent(PyReUrl).FullName);
  85.  
  86. FileStream Fs = new FileStream(PyReUrl, FileMode.Create, FileAccess.Write, FileShare.Read);
  87. Sw = new StreamWriter(Fs, System.Text.Encoding.Default, );
  88.  
  89. Sw.Write(sr.ReadToEnd());//写入
  90.  
  91. success = true;
  92. }
  93. catch(Exception ex)
  94. {
  95. Sw.Close();
  96. application.Response.Write("Writer Error"+ex.Message);
  97. }
  98.  
  99. sr.Close();
  100. Sw.Close();
  101. Response.Close();
  102. }
  103. }
  104. else
  105. {
  106. //application.Response.Redirect(PyReUrl);//链接到静态页面 浏览器请求路径不变,不会影响收录
  107. application.Server.Transfer("/Cache/" + i + "/" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(reqUrl, "MD5") + ".html");
  108. }
  109. }
  110. }
  111. }
  112.  
  113. }
  114. public void Dispose()
  115. {
  116. }
  117. }
  118. }

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

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

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

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

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

个人解决方案:

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

说下个人的解决办法把。

上代码

  1. application.Response.Write(File.ReadAllText(application.Server.MapPath("/qq534550354/" + i + "/" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(reqUrl, "MD5") + ".html")));
  2. 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. Hadoop平台安装前准备

    集群配置 准备工作 1.  Iptables #chkconfig iptables –list #chkconfig iptables –level 3456off #service iptable ...

  2. django virtualenv

    1. virtualenv virtualenv用于创建独立的Python环境,多个Python相互独立,互不影响,它能够:1. 在没有权限的情况下安装新套件2. 不同应用可以使用不同的套件版本3. ...

  3. 推荐font-size的单位 % em单位

    在如今这个提倡可用性设计以及用户体验设计的网络时代,CSS也是要一同参与其中的.大部分人在CSS代码编写中总是先对整体定义字体尺寸,中文情况下一般为12px,而其实这样以来在通过IE顶部菜单中的“察看 ...

  4. BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...

  5. 二分图最大匹配 hdoj 1045

    题目:hdoj1045 题意:给出一个图.当中有 . 和 X 两种,. 为通路,X表示墙,在当中放炸弹,然后炸弹不能穿过墙.问你最多在图中能够放多少个炸弹? 分析:这道题目是在上海邀请赛的题目的数据简 ...

  6. iReport5.6.0 linechart 制作方法

    iReport 官网和文档上关于chart设计以饼图和JDBC源作为样例.但很多其它的情况下因为报表中的数据须要首先加工处理,因此很多其它的是从JavaBeans set datasource从获取数 ...

  7. ThinkPHP - 关联模型 - 一对多

    使用之前,先引入文件夹,否则相应的功能不能实现. 如果对thinkPHP不精通,使用或开发的时候,最好直接使用完成版本的ThinkPHP. 关系模型定义: <?php /** * 继承自 Rel ...

  8. week4_motion_of_ball_1(小球运动)——最基本

    # Ball motion with an explicit timer import simplegui # Initialize globals width = 600 height = 600 ...

  9. 动态规划---最长上升子序列问题(O(nlogn),O(n^2))

    LIS(Longest Increasing Subsequence)最长上升子序列 或者 最长不下降子序列.很基础的题目,有两种算法,复杂度分别为O(n*logn)和O(n^2) . ******* ...

  10. BZOJ 3401: [Usaco2009 Mar]Look Up 仰望( 单调栈 )

    n <= 105 , 其实是10 ^ 5 ....坑...我一开始写了个模拟结果就 RE 了.. 发现这个后写了个单调栈就 A 了... ---------------------------- ...