最简单的静态页生成,好用,实用

 protected void Page_Load(object sender, EventArgs e)
    {
        //要生成html页面的aspx页面
        string url = @"http://www.mamogu.com/index.asp";
        //html页面文件名

        string fna = Server.MapPath("html/index.html");
        if (CreateList(url, fna))
        {
            Response.Write("<p>生成首页成功:" + fna);
        }

}

 //静态页生成方法 

静态页生成方法
private bool CreateList(string url, string fna)
{
bool ok;
//准备生成
string strHtml;
StreamReader sr
= null; //用来读取流

StreamWriter sw
= null; //用来写文件
Encoding code = Encoding.GetEncoding("utf-8"); //定义编码
//构造web请求,发送请求,获取响应
WebRequest HttpWebRequest = null;
WebResponse HttpWebResponse
= null;
HttpWebRequest
= WebRequest.Create(url);
HttpWebResponse
= HttpWebRequest.GetResponse();

//获得流

sr
= new StreamReader(HttpWebResponse.GetResponseStream(), code);
strHtml
= sr.ReadToEnd();

//写入文件
try
{
sw
= new StreamWriter(fna, false, code);
sw.Write(strHtml);
sw.Flush();
ok
= true;
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(
"<p>写入文件出错:" + ex.Message);
HttpContext.Current.Response.End();
ok
= false;
}
finally
{
sw.Close();
}
return ok;
}

 

 

 

posted @ 2010-12-09 23:01  金码  阅读(577)  评论(0编辑  收藏  举报