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
}

 

posted @ 2012-12-25 22:18  五百万  阅读(177)  评论(0编辑  收藏  举报