/// 生成静态网页
/// </summary>
/// <param name="str_Url">htm文件的物理地址 要完整的</param>
/// <param name="str_U">网址要完整的</param>
public void jtwy(string str_U, string str_Url)
{
try
{
if (File.Exists(str_Url) == true)
{
File.Delete(str_Url);
}
// ===建立流文件===
System.IO.StreamWriter s = new StreamWriter((System.IO.Stream)File.OpenWrite(str_Url), System.Text.Encoding.GetEncoding("GB2312"));
string cFetchStr = "";
string cException = "";
HttpWebRequest httpRequest;
httpRequest = (HttpWebRequest)WebRequest.Create(str_U);
httpRequest.Accept = "*/*";
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
System.Text.Encoding strCoding = Encoding.Default; //获取当前页的编码
StreamReader sResponseStream = new StreamReader(httpResponse.GetResponseStream(), strCoding);
cFetchStr = sResponseStream.ReadToEnd();
httpResponse.Close();
sResponseStream.Close();
s.WriteLine(cFetchStr); // 生成文件
s.Flush();
s.Close();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
public void FetchHtml(string str_U, string str_Url)
{
//str_Url htm文件的物理地址
//str_U aspx文件的路京
if (File.Exists(str_Url) == true)
{
File.Delete(str_Url);
}
// ===建立流文件===
System.IO.StreamWriter s = new StreamWriter((System.IO.Stream)File.OpenWrite(str_Url), System.Text.Encoding.GetEncoding("GB2312"));
string cFetchStr = "";
string cException = "";
HttpWebRequest httpRequest;
try
{
httpRequest = (HttpWebRequest)WebRequest.Create(str_U);
httpRequest.Accept = "*/*";
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
System.Text.Encoding strCoding = Encoding.Default; //获取当前页的编码
StreamReader sResponseStream = new StreamReader(httpResponse.GetResponseStream(), strCoding);
cFetchStr = sResponseStream.ReadToEnd();
httpResponse.Close();
sResponseStream.Close();
}
catch (Exception ee)
{
}
s.WriteLine(cFetchStr); // 生成文件
s.Flush();
s.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
FetchHtml("http://whseopm.sh09.host.35.com/default1.aspx", MapPath("../index.html"));
ClientScript.RegisterStartupScript(this.GetType(), "", string.Format("<script>alert('生成成功')</script>"));
}
来源:https://www.cnblogs.com/xiahuacheng/archive/2009/04/17/1437943.html