using System;
class StaticPage
{
/// <summary>
/// 采用直接输出法生成静态页
/// </summary>
public static void GetByRedirectOut()
{
// 通过Sql语句查询数据库中所有用户的ID,用于做URL中传递的标识
string sql = "select UserID from UserInfo";
SqlDataReader dr = SqlHelper.Instance.ExecuteReader(sql, CommandType.Text);
// 定义将来要访问的动态页的路径,开发文件系统类别的网站时会带有端口
string url = "http://localhost:1856/Web/ShowUser.aspx?uid=";
string userId = string.Empty;
// HttpWebRequest用来与服务器交互
HttpWebRequest request = null;
// 服务器响应的相关网诈
HttpWebResponse response = null;
Stream stream = null;
StreamReader reader = null;
while (dr.Read())
{
userId = dr[0].ToString();
// 模拟客户端请求,生成HttpWebRequest类实例
request = (HttpWebRequest)WebRequest.Create(url + userId);
// 获取来自服务器的响应
response = (HttpWebResponse)request.GetResponse();
stream = respose.GetResponseStream();
reader = new SteamReader(stream);
//一次性读取整个响应的内容
string result = reader.ReadToEnd();
reader.Close(); // 关闭字符流对象
response.Close(); // 关闭HttpWebResponse对象
// 生成的静态页的保存路径
string savePathForStaticPage = Server.MapPath("~/StaticPage/UserInfo" + userId + ".html");
StreamWriter writer = new StreamWriter(savePathForStaticPage, false, Encoding.Utf - 8);
writer.Write(result);
writer.Close();
}
dr.Close();
lblStatus.Text = "生成所有静态页面成功!";
}
/// <summary>
/// 采用模板替换法生成静态页
/// </summary>
public static void GetByTemplate()
{
}
}