liqiang665

  博客园 :: 首页 :: 联系 :: 订阅 订阅 :: 管理
  27 Posts :: 0 Stories :: 14 Comments :: 3 Trackbacks

公告

2008年3月20日 #

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;

/// <summary>
/// WriteHTML 的摘要说明
/// </summary>
public class WriteHTML
{
    public WriteHTML()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    public static string WriteHTMLFile(string strURL)
    {
        //保存HTML文件的路径,根目录下的html文件夹
        string strHTMLPath = System.Web.HttpContext.Current.Server.MapPath("/html/");

        //添加时间
        string addTime = System.DateTime.Now.ToFileTime().ToString();

        //编码
        Encoding code = Encoding.UTF8;

        //得到需要转成html文件的地址
        System.Net.HttpWebRequest wr = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strURL);
        wr.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24";
        wr.Method = "Get";
        System.Net.HttpWebResponse wp;
        try
        {
            wp = (System.Net.HttpWebResponse)wr.GetResponse();
        }
        catch (System.Net.WebException ex)
        {
           //输出错误
            wp = (System.Net.HttpWebResponse)ex.Response;
        }
        StreamReader sr = null;
        StreamWriter sw = null;
        string tempstr = "";

        try
        {
            //读出输出文件的文件流
            sr = new StreamReader(wp.GetResponseStream(), code);
            tempstr = sr.ReadToEnd();
        }
        catch (Exception ex)
        {
            System.Web.HttpContext.Current.Response.Write("读取错误:" + ex.Message);
        }

        //文件存储在html文件夹下,需要把修改JS,CSS,IMAGE的路径
        tempstr = tempstr.Replace("JS/Public_Funs.js", "../JS/Public_Funs.js").Replace("JS/HTTPXml.js", "../JS/HTTPXml.js").Replace("JS/ChangeStyle.js", "../JS/ChangeStyle.js").Replace("css/style1.css", "../css/style1.css").Replace("images/", "../images/");

        try
        {
            string HtmlFileName = addTime + ".html";
            //写入新文件,如果已经存在则覆盖
            sw = new StreamWriter((strHTMLPath + HtmlFileName), false, code);
            sw.Write(tempstr);
            sw.Flush();
            return HtmlFileName;
        }
        catch (Exception ex)
        {
            System.Web.HttpContext.Current.Response.Write("保存HTML文件错误:" + ex.Message);
            return "生成文件失败";
        }

    }
}

posted @ 2008-03-20 14:06 青铜时代 阅读(161) 评论(1) 编辑