• asp.net 生成静态页


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Net;
    using System.Text;
    using System.IO;
    using System.Text.RegularExpressions;
    
    /// <summary>
    ///Class1 的摘要说明
    /// </summary>
    public class Class1
    {
    	public Class1()
    	{
    		//
    		//TODO: 在此处添加构造函数逻辑
    		//
    	}
    
        #region create html method
        /// <summary>
        /// 生成静态页
        /// </summary>
        /// <param name="strURL">详细页的url地址</param>
        /// <param name="strEncode">编码类型</param>
        /// <param name="path">生成的静态页路径</param>
        /// <returns></returns>
        public static void CreateHtml(String prosmeId)
        {
            #region initialize
            String strURL = String.Format("http://bbs.csdn.net/topics/210003662", prosmeId);
            String strEncode = "UTF-8";
           
            String path = HttpContext.Current.Server.MapPath(String.Format("/webpage/{0}/{1}.htm","aa", prosmeId));
            #endregion
    
            HttpWebRequest myHttpWebRequest = null;
            try
            {
                myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
                myHttpWebRequest.AllowAutoRedirect = true;
                myHttpWebRequest.MaximumAutomaticRedirections = 5;
                myHttpWebRequest.Timeout = 100000;
                Encoding encode = Encoding.GetEncoding(strEncode);
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                if (myHttpWebRequest.HaveResponse)
                {
                    if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
                    {
                        Stream streamResponse = myHttpWebResponse.GetResponseStream();
                        StreamReader streamRead = new StreamReader(streamResponse, encode);
                        Char[] readBuff = new Char[256];
                        int count = streamRead.Read(readBuff, 0, 256);
                        StringBuilder buffer = new StringBuilder();
                        while (count > 0)
                        {
                            String outputData = new String(readBuff, 0, count);
                            buffer.Append(outputData);
                            count = streamRead.Read(readBuff, 0, 256);
                        }
    
                        streamResponse.Close();
                        streamRead.Close();
                        myHttpWebResponse.Close();
    
                        //内容html
                        string str = buffer.ToString();
                        //清除视图状态
                        str = FilterViewState(str);
    
                        lock (typeof(HtmlCreateHelper))
                        {
                            SaveFile(str, path, encode);
                        }
                    }
                }
                else
                {
               ToLog(strURL, null, "没有响应");
                }
            }
            catch (Exception ex)
            {
                ToLog(strURL, ex, null);
            }
            finally
            {
                myHttpWebRequest.Abort();
                myHttpWebRequest = null;
            }
        }
        #endregion
    
        #region save html file
        private static object ioObj = new object();
        private static bool SaveFile(string strMsg, string strFilePath, Encoding encode)
        {
            bool isOk = false;
            lock (ioObj)
            {
                strMsg = strMsg.Trim();
                if (strMsg == "")
                {
                    throw new Exception("写入文件长度为0");
                }
                if (!Directory.Exists(Path.GetDirectoryName(strFilePath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(strFilePath));
                }
                using (StreamWriter write = new StreamWriter(strFilePath, false, encode))
                {
                    write.Write(strMsg);
                    isOk = true;
                }
            }
            return isOk;
        }
        #endregion
    
        #region write into log file
        private static void ToLog(String renderURL, Exception err, string errMsg)
        {
            StringBuilder logInfo = new StringBuilder();
            logInfo.Append("\n").Append("生成静态错误:\n");
            if (!string.IsNullOrEmpty(errMsg))
            {
                logInfo.Append(errMsg).Append("\n");
            }
            logInfo.Append(renderURL).Append("\n");
            if (err != null)
            {
                //logger.Error(logInfo.ToString());
            }
            else
            {
                //logger.Error(logInfo.ToString(), err);
            }
        }
        #endregion
    
        #region clear viewstate
        private static string FilterViewState(string html)
        {
            string regexstr = "<input type=\"hidden\" name=\"(__VIEWSTATE|__EVENTTARGET|__EVENTARGUMENT|__CustomViewState)\"[^>]*?>";
            return Regex.Replace(html, regexstr, "", RegexOptions.IgnoreCase);
        }
        #endregion
    }
    
  • 相关阅读:
    #define、const、typedef的区别
    《软件调试的艺术》学习笔记——GDB使用技巧摘要
    作为员工与老板的竞争力区别
    部分经典IT书籍
    对找工作功不可没——评《深入理解计算机系统》
    前端UI框架小汇总
    Visual Studio 2017 序列号 Key 激活码 VS2017 注册码
    Wampserver 2.5 多站点配置方法
    ThinkPHP3.1快速入门(9)变量输出
    ThinkPHP3.1快速入门(8)视图
  • 原文地址:https://www.cnblogs.com/jj123/p/2833134.html
Copyright © 2020-2023  润新知