生成靜態頁 批量或單個
阿新 • • 發佈:2019-01-02
生成靜態頁的類,可以批量或者單個生成,(經過測試)
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("失敗 ");
}
}