两种方法
/**/
/// <summary>
/// 静态生成页面的方法
/// </summary>
/// <param name='strPageUrl'>生成源</param>
/// <param name='strFileName'>生成到</param>
private
bool
MakePage(
string
strPageUrl,
string
strFileName)

{
string strDir,strFilePage;
strDir = @'/htm/MakePage/';//更新到的文件夹
strFilePage = Server.MapPath(strDir+strFileName);

StreamWriter sw=null;
//获得aspx的静态html
try

{
if (!Directory.Exists(Server.MapPath(strDir)))

{
Directory.CreateDirectory(Server.MapPath(strDir));
}
if(File.Exists(strFilePage))

{
File.Delete(strFilePage);
}
sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding('GB2312'));
Server.Execute(strPageUrl,sw);
}
catch(Exception ex)

{
msg.ShowMsg('''+strFileName+''生成出错:'+ex.Message);
return false;//生成到出错
}
finally

{
sw.Flush();
sw.Close();
sw = null;
}

return true;
}
方法二:
/**/
/// <summary>
/// 静态生成页面的方法
/// </summary>
/// <param name='strPageUrl'>生成源</param>
/// <param name='strFileName'>生成到</param>
private
bool
MakePage(
string
strPageUrl,
string
strFileName)

{
string strDir,strFilePage;
strDir = @'/htm/MakePage/';//更新到的文件夹
strFilePage = Server.MapPath(strDir+strFileName);

StreamWriter sw=null;
//获得aspx的静态html
try

{
if (!Directory.Exists(Server.MapPath(strDir)))

{
Directory.CreateDirectory(Server.MapPath(strDir));
}
if(File.Exists(strFilePage))

{
File.Delete(strFilePage);
}
sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding('GB2312'));
System.Net.WebRequest wReq = System.Net.WebRequest.Create(strPageUrl);
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('gb2312'));
sw.Write( reader.ReadToEnd());
}
catch(Exception ex)

{
msg.ShowMsg('''+strFileName+''生成出错:'+ex.Message);
return false;//生成到出错
}
finally

{
sw.Flush();
sw.Close();
sw = null;
}

return true;
}
总结:方法一只可用用虚拟路径(如:不能用 http://www.qq.com),而方法二相反。
方法一:
/**/
/// <summary>
/// 静态生成页面的方法
/// </summary>
/// <param name='strPageUrl'>生成源</param>
/// <param name='strFileName'>生成到</param>
private
bool
MakePage(
string
strPageUrl,
string
strFileName)
{
string strDir,strFilePage;
strDir = @'/htm/MakePage/';//更新到的文件夹
strFilePage = Server.MapPath(strDir+strFileName);
StreamWriter sw=null;
//获得aspx的静态html
try
{
if (!Directory.Exists(Server.MapPath(strDir)))
{
Directory.CreateDirectory(Server.MapPath(strDir));
}
if(File.Exists(strFilePage))
{
File.Delete(strFilePage);
}
sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding('GB2312'));
Server.Execute(strPageUrl,sw);
}
catch(Exception ex)
{
msg.ShowMsg('''+strFileName+''生成出错:'+ex.Message);
return false;//生成到出错
}
finally
{
sw.Flush();
sw.Close();
sw = null;
}
return true;
}
/**/
/// <summary>
/// 静态生成页面的方法
/// </summary>
/// <param name='strPageUrl'>生成源</param>
/// <param name='strFileName'>生成到</param>
private
bool
MakePage(
string
strPageUrl,
string
strFileName)
{
string strDir,strFilePage;
strDir = @'/htm/MakePage/';//更新到的文件夹
strFilePage = Server.MapPath(strDir+strFileName);
StreamWriter sw=null;
//获得aspx的静态html
try
{
if (!Directory.Exists(Server.MapPath(strDir)))
{
Directory.CreateDirectory(Server.MapPath(strDir));
}
if(File.Exists(strFilePage))
{
File.Delete(strFilePage);
}
sw = new StreamWriter(strFilePage, false, System.Text.Encoding.GetEncoding('GB2312'));
System.Net.WebRequest wReq = System.Net.WebRequest.Create(strPageUrl);
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('gb2312'));
sw.Write( reader.ReadToEnd());
}
catch(Exception ex)
{
msg.ShowMsg('''+strFileName+''生成出错:'+ex.Message);
return false;//生成到出错
}
finally
{
sw.Flush();
sw.Close();
sw = null;
}
return true;
}
总结:方法一只可用用虚拟路径(如:不能用 http://www.qq.com),而方法二相反。
本文介绍了两种不同的静态页面生成方法,方法一通过Server.Execute直接输出ASPX页面为HTML,适用于虚拟路径;方法二则利用WebRequest从指定URL抓取内容并保存为静态页面,适合完整URL路径。两种方法各有优缺点。
本站推荐

被折叠的 条评论
为什么被折叠?



