生成静态页的类,可以批量或者单个生成,(经过测试)
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
namespace JobFuntion
{
/// <summary>
/// 生成静态页
/// </summary>
public class WhishtPage
{
private static readonly string _Company = "Company"; //企业招聘信息 静态页名字
private static readonly string _Invite = "Invite"; //个人建立信息 静态页名字
private static readonly string _Index = "Index"; //网站首页 静态页名字
private static readonly string _Encoding = "gb2312"; //编码
private static readonly string _IndexUrl = "http://localhost/JobNetUI/Default.aspx"; //获取首页的地址
private static readonly string _IndexSaveUrl = "../JobNetUI/"; //保存首页的地址
private static readonly string _CompanyUrl = "http://localhost/JobNetUI/Default.aspx"; //获取招聘信息的值得
private static readonly string _CompanySaveUrl = "../JobNetUI/Company/StatePage/"; //保存招聘信息的地址
private static readonly string _InviteUrl = "http://localhost/JobNetUI/Default.aspx"; //个人简历的地址
private static readonly string _InviteSaveUrl = "../JobNetUI/Invite/StatePage/"; //保存个人简历的地址
/// <summary>
/// 生成静态页
/// </summary>
/// <param name="ID">数据ID</param>
/// <param name="UrlAdd">要生成的页面</param>
/// <param name="SaveUrl">保存的地址</param>
/// <param name="FileName">文件名称</param>
/// <param name="EncodingCode">编码</param>
///
protected static int StatePage(string UrlAdd, string Saveadd, string Name, string EncodingCode)
{
try
{
System.Net.WebRequest HttpWebRequest = System.Net.WebRequest.Create(UrlAdd);
System.Net.WebResponse HttpWebResponse = HttpWebRequest.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(HttpWebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding(EncodingCode));
string FileName = Name + ".html";
string ffname = System.Web.HttpContext.Current.Server.MapPath(Saveadd) + FileName;
// string ffname = FileName;
string txthtm = sr.ReadToEnd();
//this.TextBox1.Text= sr.ReadToEnd();
//创建文件信息对象
FileInfo finfo = new FileInfo(ffname);
//以打开或者写入的形式创建文件流
using (FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
//StreamWriter sw = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath(Saveadd) + FileName, false, System.Text.Encoding.GetEncoding(EncodingCode));
//把新的内容写到创建的HTML页面中
//sw.WriteLine(txthtm);
sw.WriteLine(txthtm);
sw.Flush();
sw.Close();
}
return 1;
}
catch(Exception ex)
{
return -1;
}
}
/// <summary>
/// 生成首页
/// </summary>
/// <returns></returns>
public static int Index()
{
return StatePage(_IndexUrl,_IndexSaveUrl, _Index, _Encoding);
}
/// <summary>
/// 生成企业信息(单条)
/// </summary>
/// <param name="ID">企业ID</param>
/// <returns>生成单条企业信息</returns>
public static int Company(int ID)
{
string StrCompanyUrl = _CompanyUrl;
// StrCompanyUrl = StrCompanyUrl + "?id=" + ID;
return StatePage(StrCompanyUrl, _CompanySaveUrl, _Company + ID, _Encoding);
}
/// <summary>
/// 生成企业信息(批量)
/// </summary>
/// <param name="BeginID">开始的ID</param>
/// <param name="EndID">结束ID</param>
/// <returns>批量生成企业信息</returns>
public static int Company(int BeginID, int EndID)
{
string StrCompanyUrl = _CompanyUrl;
try
{
for (int i = BeginID; i < EndID;i++ )
{
// StrCompanyUrl = StrCompanyUrl + "?id=" + i;
StatePage(StrCompanyUrl,_CompanySaveUrl,_Company+i,_Encoding);
}
return 1;
}
catch
{
return -1;
}
}
/// <summary>
/// 生成简历信息(单条)
/// </summary>
/// <param name="ID">简历ID</param>
/// <returns></returns>
public static int Invite(int ID)
{
string StrInviteUrl = _InviteUrl;
// StrInviteUrl = StrInviteUrl = "?id=" + ID;
return StatePage(StrInviteUrl, _InviteSaveUrl, _Invite + ID, _Encoding);
}
/// <summary>
/// 生成个人简历(批量)
/// </summary>
/// <param name="BeginID">开始ID</param>
/// <param name="EndID">结束ID</param>
/// <returns></returns>
public static int Invite(int BeginID, int EndID)
{
string StrInviteUrl = _InviteUrl;
try
{
for (int i = BeginID; i < EndID; i++)
{
// StrInviteUrl = StrInviteUrl + "?id=" + i;
StatePage(StrInviteUrl,_InviteSaveUrl, _Invite+i, _Encoding);
}
return 1;
}
catch
{
return -1;
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int err = JobFuntion.WhishtPage.Index();
if (err == 1)
{
Response.Write("成功");
}
else
{
Response.Write("失败 ");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
int err = JobFuntion.WhishtPage.Company(11);
if (err == 1)
{
Response.Write("成功");
}
else
{
Response.Write("失败 ");
}
}
protected void Button3_Click(object sender, EventArgs e)
{
int err = JobFuntion.WhishtPage.Company(Convert.ToInt32(TextBox1.Text),Convert.ToInt32(TextBox2.Text));
if (err == 1)
{
Response.Write("成功");
}
else
{
Response.Write("失败 ");
}
}
protected void Button4_Click(object sender, EventArgs e)
{
int err = JobFuntion.WhishtPage.Invite(11);
if (err == 1)
{
Response.Write("成功");
}
else
{
Response.Write("失败 ");
}
}
protected void Button5_Click(object sender, EventArgs e)
{
int err = JobFuntion.WhishtPage.Invite(Convert.ToInt32(TextBox3.Text),Convert.ToInt32(TextBox4.Text));
if (err == 1)
{
Response.Write("成功");
}
else
{
Response.Write("失败 ");
}
}