兩種方法
方法一:
/**//// <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),而方法二相反。