载入中...
 
     
 
载入中...
最新评论
载入中...
专题分类
载入中...
最新日志
载入中...
最新留言
载入中...
搜索
友情链接
博客信息
载入中...


 
 
载入中...
   
 
 
.net 根据url生成静态html文件 
[ 2011-11-28 10:35:00 | By: gaoxiaopeng ]
 

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-

transitional.dtd">

< runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //url 为您将要生成html 的动态页面
            string url = "http://sjfzxm.com";
            string index = getUrltoHtml(url);

            //下面是获取要保持文件的路径 如果要生成上一级文件夹中请修改 ../index.htm   注意 index.htm 是iis 的默认,

即使不设置起始页面输入域名也能直接被访问到 。index.html 却不是  
            string fileName = Server.MapPath("index.htm");
            bool result = SaveStrTofile(fileName, index, "utf-8");


            if (result)
            {
                Response.Write("生成" + url + "成功");
            }
            else
            {
                Response.Write("生成" + url + "失败");
            }
        }
      
    }
    /// <summary>
    /// 传入网页URL返回网页的html代码
    /// </summary>
    /// <param name="Url">网页URL,必须是http://开始的</param>
    /// <returns></returns>
    public  string getUrltoHtml(string Url)
    {
        string errorMsg = "";
        try
        {
            System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
            System.Net.WebResponse wResp = wReq.GetResponse();
            System.IO.Stream respStream = wResp.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding

("utf-8"));
            return reader.ReadToEnd();

        }
        catch (System.Exception ex)
        {
            errorMsg = ex.Message;
        }
        return "";
    }


    /// <summary>
    /// 保持文件:成功,'true' or 'false' 文件路径,编码方式utf-8,gb2312
    /// </summary>
    /// <param name="filename">文件路径</param>
    /// <param name="StrContTent">保存文件内容</param>
    /// <param name="code">保持编码方式</param>
    /// <returns>bool</returns>
    public  bool SaveStrTofile(string filename, string StrContTent, string code)
    {
        bool boolstr = false;
        Encoding codestr = Encoding.GetEncoding(code);
        System.IO.StreamWriter sw=null;

        try
        {
         
            sw = new System.IO.StreamWriter(filename, false, codestr);

            sw.Write(StrContTent);

            sw.Flush();
            boolstr = true;
        }
        catch (Exception ex)
        {
            Response.Write(filename + ex);
        }
        finally
        {
            sw.Close();
        }
        return boolstr;
    }
</>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>生成静态页面</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    发布后请设置生成文件的目录的写入权限,不然会报错哦!“未实例化”

遇到乱码 请修改 编码方式  要统一哦!是utf-8  就都修改成  utf-8 .gb2312同理  

此页面代码为.aspx  代码  在新建的时候  请把“将代码放到单独的文件中(P)”前面的勾去掉

http://sjfzxm.com/ 世界服装鞋帽网技术部提供


    </div>
    </form>
</body>
</html>

 
 
  • 标签:.net 生成 url html 静态 
  • 发表评论:
    载入中...