asp.net 页面生成html页面 方法一

这是个方法
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;
}

 

调用就这样了

//要生成html页面的aspx页面
string url = @ http://www.lob.cn/list.aspx;
//html页面文件名
string fna = Server.MapPath("") + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + ".html";
if (CreateList(url, fna))
{
Response.Write("<p>生成文件成功:" + fna);
}

你可能感兴趣的:(asp.net)