using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Text;

namespace baoge.Admin
{
	/// <summary>
	/// createhtml 的摘要说明。
	/// </summary>
	public class createhtml : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox UrlText;
		protected System.Web.UI.WebControls.Button btnWebClient;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.Button btnClear;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.HyperLink hyCreateFile;
		protected System.Web.UI.WebControls.TextBox ContentHtml;
	     private string PageUrl = "";
		private void Page_Load(object sender, System.EventArgs e)
		{
//			if(Session["AdminName"]==null)
//			{
//				Response.Redirect("Admin_Login.aspx");
//			}
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnWebClient.Click += new System.EventHandler(this.btnWebClient_Click);
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnWebClient_Click(object sender, System.EventArgs e)
		{
			//取得输入的URL地址
			PageUrl = UrlText.Text;
			WebClient wc = new WebClient();
			//把已登录的用户或正被模拟的用户的用户凭据赋给用于对向Internet资源的请求进行身份验证的网络凭据
			wc.Credentials = CredentialCache.DefaultCredentials;

			///方法一：
			///从指定的URI下载数据
			Byte[] pageData = wc.DownloadData(PageUrl);
			//把下载到的字符型数组数据转换成字符串类型
			this.ContentHtml.Text = Encoding.Default.GetString(pageData);

			/// 方法二
			//为指定的URI打开一个可读的数据流，并定义一个该流的引用
			/// Stream resStream = wc.OpenRead(PageUrl);
			/// 根据上面定义的数据流，以默认编码的方式定义一个读数据流
			/// StreamReader sr = new StreamReader(resStream,System.Text.Encoding.Default);
			/// 读取数据流中的内容
			/// ContentHtml.Text = sr.ReadToEnd();
			/// //关闭数据流
			/// resStream.Close();
			/// 
			//释放资源
			wc.Dispose();  
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			StringBuilder strhtml = new StringBuilder();
			try 
			{
				//创建StreamReader对象
				using (StreamReader sr = new StreamReader(Server.MapPath("./") + "\\template.html")) 
				{
					String oneline;
					//读取指定的HTML文件模板
					while ((oneline = sr.ReadLine()) != null) 
					{
						strhtml.Append(oneline);
					}
					sr.Close();
				}
			}
			catch(Exception err)
			{
				//输出异常信息
				Response.Write(err.ToString());
			}
		
			string newContent=ContentHtml.Text;
			
			try
			{
				//指定要生成的HTML文件
//				string fname = Server.MapPath("../") +"\\" + "default.html";
				string fname = Server.MapPath("../") +"\\" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";
				//替换html模版文件里的标记为新的内容
				
				strhtml.Replace("$htmlkey",newContent);
				
				//创建文件信息对象
				FileInfo finfo = new FileInfo(fname);
				//以打开或者写入的形式创建文件流
//				using(FileStream fs = finfo.OpenWrite())
								using(FileStream fs = finfo.OpenWrite())

				{
					//根据上面创建的文件流创建写数据流
					StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));
					//把新的内容写到创建的HTML页面中
					
				    sw.WriteLine(strhtml);
//					sw.Write(strhtml);
					sw.Flush();
					sw.Close();
				}
				//设置超级链接的属性
				hyCreateFile.Text = DateTime.Now.ToString("yyyymmddhhmmss")+".html";
				hyCreateFile.NavigateUrl = "../"+DateTime.Now.ToString("yyyymmddhhmmss")+".html";
			}
			catch(Exception err)
			{ 
				Response.Write (err.ToString());
			}
		}

		private void btnClear_Click(object sender, System.EventArgs e)
		{
			this.ContentHtml.Text = "";
		}
	}
}
