ASP. NET Server.Transfer Direct output HTML source code problem

Source: Internet
Author: User

I'm using it today. NET using Ihttpmodel to implement static cache of the site, do not know why the last time with Server.Transfer (HTML) When the result is the output of HTML source code.

Paste the source code

usingSystem;usingsystem.web;usingSystem.Text.RegularExpressions;usingSystem.IO;usingSystem.Configuration;usingSystem.Collections.Generic;namespaceproduct{ Public classProductmodule:ihttpmodule { Public voidInit (HttpApplication application) {application. BeginRequest+= (NewEventHandler ( This. Application_BeginRequest));//Request StartApplication. EndRequest + = (NewEventHandler ( This. Application_EndRequest));//End of Request        }        Private voidApplication_BeginRequest (Object source, EventArgs e) {HttpApplication application=(HttpApplication) source;        Checkurl (application); }        Private voidApplication_EndRequest (Object source, EventArgs e) {//HttpApplication application = (HttpApplication) source; //Application.Response.Write ("test");        }        Private voidCheckurl (HttpApplication application) {if(Application. Request.RequestType.ToUpper () = ="POST"|| Application. Request.UserAgent.ToLower () = ="Product")            {                return; }            string[] Resurltemp =New string[5];//request template to cacheresurltemp[0] ="/model/modelsearch.aspx\\?clid= ([\\d]+) &coid= ([\\d]+) &bid= ([\\d]+] &price= (. *) &model= (. *) &p= ([\\d]+) &t= ([0-2]{1,1})"; resurltemp[1] ="/pic/imgsearch.aspx\\?clid= ([\\d]+) &cbid= ([\\d]+) &model= (. *) &p= ([\\d]+)"; resurltemp[2] ="/praise/praisesearch.aspx\\?clid= ([\\d]+) &coid= ([\\d]+) &model= (. *) &p= ([\\d]+)"; resurltemp[3] ="/price/sellerpricesearch.aspx\\?clid= ([\\d]+) &coid= ([\\d]+) &brand= ([\\d]+] &price= (. *) & Model= (. *) &p= ([\\d]+) &t= ([0-2]{1,1})"; resurltemp[4] ="/dealer/sellersearch.aspx\\?pve= ([\\d]+) &city= ([\\d]+) &type= ([\\d]+] &seller= (. *) &bid= ([\\d ]+) &model= (. *) &pagesize= ([\\d]+) &p= ([\\d]+)"; stringRequrl = Application. Context.Request.Url.PathAndQuery.ToLower ();//Request Dynamic Path                        BOOLSuccess =false;  for(inti =0; I < resurltemp.length;i++ )            {                if(!success) {Regex reg=NewRegex (Resurltemp[i]);//matches whether the current request requires cachingMatchCollection MC =Reg.                    Matches (Requrl); if(MC. Count >0)                    {                        //static page naming uses the current request path MD5 encrypted name                        stringPyreurl = configurationmanager.connectionstrings["Webphysicsurl"]. ConnectionString +"/cache/"+ i +"/"+ System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (Requrl,"MD5") +". html"; FileInfo fi=NewFileInfo (Pyreurl);//Determine if caching                        if(!fi. Exists) {//Cache Page                            stringWEBURL = configurationmanager.connectionstrings["WEBURL"].                            ConnectionString; System.Net.HttpWebRequest Request= (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create (WebUrl + requrl);//POST request current pageRequest.method ="GET"; Request.accept="*/*"; Request.useragent="Product"; Request.allowautoredirect=true; Request.maximumautomaticredirections=2; System.Net.HttpWebResponse Response=NULL; Try{Response= (System.Net.HttpWebResponse) request.getresponse ();//Get a response                            }                            Catch(Exception ex) {application. Response.Write ("Response Error"+Ex.                            Message); }                            if(Response! =NULL) {System.IO.Stream strm=Response.getresponsestream (); System.IO.StreamReader SR=NewSystem.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=NewFileStream (Pyreurl, FileMode.Create, FileAccess.Write, FileShare.Read); Sw=NewStreamWriter (Fs, System.Text.Encoding.Default, +); Sw.write (Sr. ReadToEnd ());//WriteSuccess=true; }                                Catch(Exception ex) {sw.close (); Application. Response.Write ("Writer Error"+Ex.                                Message); } Sr.                                Close ();                                Sw.close ();                            Response.close (); }                        }                        Else                        {                            //application. Response.Redirect (Pyreurl);//link to static page browser request path is unchanged and does not affect ingestApplication. Server.Transfer ("/cache/"+ i +"/"+ System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (Requrl,"MD5") +". html"); }                    }                }            }                        }         Public voidDispose () {}}}

Original address: http://blog.csdn.net/mr_tanglin/article/details/6753771

My question is this, my site uses the UTF8 encoding

When these two streams use gb2312, Service.transfer can display HTML normally, but the page is garbled

  System.IO.Stream strm = response.getresponsestream ();     New System.IO.StreamReader (STRM, System.Text.Encoding.GetEncoding ("gb2312"));  
New FileStream (Pyreurl, FileMode.Create, FileAccess.Write, fileshare.read);    New );  

But when I use UTF encoding, the page cache is fine, but Service.transfer just shows the source code and doesn't know why. Solving.

This sticker is used for recording, and if a solution is found, I will post it!

ASP. NET Server.Transfer Direct output HTML source code problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.