111,129
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
private Thread thr;
public Thread Thr
{
get
{
if (System.Web.HttpContext.Current.Application["thread"] == null)
return null;
else
return (Thread)System.Web.HttpContext.Current.Application["thread"];
}
set
{
System.Web.HttpContext.Current.Application["thread"] = value;
}
}
public CreateHTML() { }
/// <summary>
/// 生成静态页
/// </summary>
/// <param name="reqpath">请求地址</param>
/// <param name="savepath">生成路径</param>
public CreateHTML(string reqpath, string savepath)
{
if (thr == null)
{
ThreadStart starter = delegate { GenerateHtml(reqpath, savepath); };
Thread th = new Thread(starter);
th.IsBackground = false;
thr = th;
thr.Start();
}
else
{
if (thr.ThreadState.Equals(ThreadState.Aborted))
Thread.ResetAbort();
}
}
/// <summary>
/// 批量生成
/// </summary>
/// <param name="dic">key是生成地址,value是请求地址</param>
public CreateHTML(Dictionary<string,string> dic)
{
if (thr == null)
{
ThreadStart starter = delegate { GenerateHtml(dic); };
Thread th = new Thread(starter);
th.IsBackground = false;
thr = th;
thr.Start();
}
else
{
if (thr.ThreadState.Equals(ThreadState.Aborted))
Thread.ResetAbort();
}
}
#region 生成静态页
/// <summary>
/// 生成静态页
/// </summary>
/// <param name="reqpath">请求地址</param>
/// <param name="savepath">生成地址</param>
public bool BulidHtml(string reqpath, string savepath)
{
bool reslut = false;
try
{
WebRequest wr = WebRequest.Create(reqpath);
WebResponse wrs = wr.GetResponse();
Stream str = wrs.GetResponseStream();
StreamReader sr = new StreamReader(str, System.Text.Encoding.GetEncoding("utf-8"));
string abc = sr.ReadToEnd();
StreamWriter sw = null;
string path = savepath.Replace("/", @"\");
int pos = path.LastIndexOf(@"\");
if (pos != -1)
{
string dir = path.Substring(0, pos);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
try
{
sw = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("utf-8"));
sw.Write(abc);
reslut = true;
}
catch (Exception ex)
{
//throw new Exception("出错啦:" + ex.Message);
}
finally
{
wrs.Close();
str.Close();
str.Flush();
sr.Close();
sw.Flush();
sw.Close();
}
}
return reslut;
}
catch (Exception)
{
return reslut;
}
}
/// <summary>
/// 生成静态页
/// </summary>
/// <param name="reqpath">请求地址</param>
/// <param name="savepath">生成地址</param>
private void GenerateHtml(string reqpath, string savepath)
{
WebRequest wr = WebRequest.Create(reqpath);
WebResponse wrs = wr.GetResponse();
Stream str = wrs.GetResponseStream();
StreamReader sr = new StreamReader(str, System.Text.Encoding.GetEncoding("utf-8"));
string abc = sr.ReadToEnd();
StreamWriter sw = null;
string path = savepath.Replace("/", @"\");
int pos = path.LastIndexOf(@"\");
if (pos != -1)
{
string dir = path.Substring(0, pos);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
try
{
sw = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("utf-8"));
sw.Write(abc);
}
catch (Exception ex)
{
//todo:此处单独做错误处理
}
finally
{
wrs.Close();
str.Close();
str.Flush();
sr.Close();
sw.Flush();
sw.Close();
thr.Abort();
}
}
}
/// <summary>
/// 批量生成静态页
/// </summary>
/// <param name="dic">key是生成地址,value是请求地址</param>
private void GenerateHtml(Dictionary<string,string> dic)
{
foreach (string savepath in dic.Keys)
{
string reqpath = dic[savepath];
WebRequest wr = WebRequest.Create(reqpath);
WebResponse wrs = wr.GetResponse();
Stream str = wrs.GetResponseStream();
StreamReader sr = new StreamReader(str, System.Text.Encoding.GetEncoding("utf-8"));
string abc = sr.ReadToEnd();
StreamWriter sw = null;
string path = savepath.Replace("/", @"\");
int pos = path.LastIndexOf(@"\");
if (pos != -1)
{
string dir = path.Substring(0, pos);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
try
{
sw = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("utf-8"));
sw.Write(abc);
}
catch (Exception ex)
{
//todo:此处单独做错误处理
}
finally
{
wrs.Close();
str.Close();
str.Flush();
sr.Close();
sw.Flush();
sw.Close();
}
}
}
thr.Abort();
}
#endregion