• 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
    }
    
  • 相关阅读:
    设计模式学习——代理模式(Proxy Pattern)之 强制代理(强校验,防绕过)
    设计模式学习——代理模式(Proxy Pattern)
    设计模式学习——抽象工厂模式(Abstract Factory Pattern)
    最长字符串系列汇总
    窗口的最大值与最小值更新结构(滑动窗口)
    归并排序和归并排序应用(逆序对+小和)
    位运算在编程题的一些作用
    链表的排序(归并排序+快慢指针)
    Manacher算法解决最长回文子串长度问题
    回文数字的验证
  • 原文地址:https://www.cnblogs.com/jj123/p/2833134.html
Copyright © 2020-2023  润新知