[code=C#]
#region 生成首页静态页 /// <summary>
/// 生成首页静态页
/// </summary>
/// <returns></returns>
public static bool makeIndexPage()
{
//生成HTML文件
return GetPageHTML.makeHTML("http://" + webNames + "/aspxTemplate/Default.aspx", "/index.html");
}
#endregion 生成首页静态页
using System;
namespace WebBase
{
public class GetPageHTML
{
public static bool makeHTML(string urls, string filename)
{
try
{
return WriteStart(filename,getPageHTML(urls, Encoding.GetEncoding("UTF-8")));
}
catch(Exception ex)
{
//return false;
throw new Exception(ex.Message);
}
}
/// <summary>
/// 得到完整的HTML代码
/// </summary>
/// <param name="urls"></param>
/// <returns></returns>
public static string getPageHTML(string urls,Encoding encode)
{
string html = null;
HttpWebRequest request = WebRequest.Create(urls) as HttpWebRequest;
request.Method = "GET";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(response.GetResponseStream(), encode))
{
html = sr.ReadToEnd();
//这里必须关闭,否则内存占用增长....
sr.Close();
}
response.Close();
request.Abort();
response = null;
request = null;
return html;
}
#region 替换网页中的换行和引号
/* 函数名称:ReplaceEnter
* 功能说明:替换网页中的换行和引号
* 参 数:HtmlCode:html源代码
* 调用示例:
* GetRemoteObj o = new GetRemoteObj();
* string Url = @"http://www.baidu.com";
* string HtmlCode = o.GetRemoteHtmlCode(Url);
* string s = o.ReplaceEnter(HtmlCode);
* Response.Write(s);
* o.Dispose();
* ********************************/
/// <summary>
/// <param name="HtmlCode">HTML源代码</param>
/// <returns></returns>
/// </summary>
public static string ReplaceEnter(string HtmlCode)
{
string s = "";
if (HtmlCode == null || HtmlCode == "")
s = "";
else
s = HtmlCode.Replace("\"", "");
s = s.Replace("\r", "");
s = s.Replace("\n", "");
return s;
}
#endregion 替换网页中的换行和引号
#region 执行正则提取出值
/**/
/**********************************
* 函数名称:GetRegValue
* 功能说明:执行正则提取出值
* 参 数:HtmlCode:html源代码
* 调用示例:
* GetRemoteObj o = new GetRemoteObj();
* string Url = @"http://www.baidu.com";
* string HtmlCode = o.GetRemoteHtmlCode(Url);
* string s = o.ReplaceEnter(HtmlCode);
* string Reg="<title>.+?</title>";
* string GetValue=o.GetRegValue(Reg,HtmlCode)
* Response.Write(GetValue);
* o.Dispose();
* ********************************/
/**/
/// <summary>
/// <param name="RegexString">正则表达式</param>
/// <param name="RemoteStr">HtmlCode源代码</param>
/// <returns></returns>
/// </summary>
public static string GetRegValue(string RegexString, string RemoteStr)
{
string MatchVale = "";
Regex r = new Regex(RegexString);
Match m = r.Match(RemoteStr);
if (m.Success)
{
MatchVale = m.Value;
}
return MatchVale;
}
#endregion 执行正则提取出值
#region 获取远程文件源代码
/**/
/**********************************
* 函数名称:GetRemoteHtmlCode
* 功能说明:获取远程文件源代码
* 参 数:Url:远程url
* 调用示例:
* GetRemoteObj o = new GetRemoteObj();
* string url = @"http://www.baidu.com";
* string s = o.GetRemoteHtmlCode(url);
* Response.Write(s);
* o.Dispose();
* ********************************/
/**/
/// <summary>
/// <param name="url">远程url</param>
/// <returns></returns>
/// </summary>
public static string GetRemoteHtmlCode(string Url)
{
string s = "";
MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
_xmlhttp.open("GET", Url, false, null, null);
_xmlhttp.send("");
if (_xmlhttp.readyState == 4)
{
s = System.Text.Encoding.Default.GetString((byte[])_xmlhttp.responseBody);
}
return s;
}
#endregion 获取远程文件源代码
#region 保存远程文件
/**/
/**********************************
* 函数名称:RemoteSave
* 功能说明:保存远程文件
* 参 数:Url:远程url;Path:保存到的路径
* 调用示例:
* GetRemoteObj o = new GetRemoteObj();
* string s = "";
* string url = @"
* string path =Server.MapPath("Html/");
* s = o.RemoteSave(url,path);
* Response.Write(s);
* o.Dispose();
* ******************************/
/**/
/// <summary>
/// 保存远程文件 ///这个有问题,不能使用
/// </summary>
/// <param name="Url">远程url</param>
/// <param name="Path">保存到的路径</param>
/// <returns></returns>
public static string RemoteSave(string Url, string Path)
{
Random ra = new Random();
string newfilename = string.Empty;
string StringFileName = DateRndName(ra) + "." + GetFileExtends(Url);
if (GetFileExtends(Url).Trim().ToLower() != "jpg")
newfilename = StringFileName;
else
newfilename = DateRndName(ra) + "Addpic." + GetFileExtends(Url);
string StringFilePath = Path + StringFileName;
string newfilepath = Path + newfilename;
string retname = string.Empty;
try
{
MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
_xmlhttp.open("GET", Url, false, null, null);
_xmlhttp.send("");
if (_xmlhttp.readyState == 4)
{
if (System.IO.File.Exists(StringFilePath))
System.IO.File.Delete(StringFilePath);
System.IO.FileStream fs = new System.IO.FileStream(StringFilePath, System.IO.FileMode.CreateNew);
System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs);
w.Write((byte[])_xmlhttp.responseBody);
w.Close();
fs.Close();
if (GetFileExtends(Url).Trim().ToLower() == "jpg")
{
//function myfunction = new function();
////myfunction.AddShuiYinWord(StringFilePath, newfilepath);
//service myserver=new service();
//myfunction.AddShuiYinPic(StringFilePath, newfilepath, HttpContext.Current.Server.MapPath(myserver.myweblogo));
}
}
else
throw new Exception(_xmlhttp.statusText);
}
catch (Exception ex)
{
}
return newfilename;
}
#endregion 保存远程文件
#region 根据日期取得一个随机串
/// <summary>
/// 取得一个随机字符串
/// </summary>
/// <returns></returns>
private static string DateRndName(Random ra)
{
DateTime d = DateTime.Now;
string s = null, y, m, dd, h, mm, ss;
y = d.Year.ToString();
m = d.Month.ToString();
if (m.Length < 2) m = "0" + m;
dd = d.Day.ToString();
if (dd.Length < 2) dd = "0" + dd;
h = d.Hour.ToString();
if (h.Length < 2) h = "0" + h;
mm = d.Minute.ToString();
if (mm.Length < 2) mm = "0" + mm;
ss = d.Second.ToString();
if (ss.Length < 2) ss = "0" + ss;
s += y + ',' + m + ',' + dd + ',' + h + "-" + mm + "-" + ss;
s += ra.Next(1000000, 9999999).ToString();
return s;
}
#endregion 根据日期取得一个随机串
#region 取得文件扩展名
private static string GetFileExtends(string filename)
{
if (filename.IndexOf(".") > 0)
{
return filename.Substring(filename.LastIndexOf("."));
}
else
{
return filename;
}
}
#endregion 取得文件扩展名
#region 替换HTML源代码
/**/
/**********************************
* 函数名称:RemoveHTML
* 功能说明:替换HTML源代码
* 参 数:HtmlCode:html源代码
* 调用示例:
* GetRemoteObj o = new GetRemoteObj();
* string Url = @"http://www.baidu.com";
* string HtmlCode = o.GetRemoteHtmlCode(Url); 中国网管联盟bitsCN.com
* string s = o.ReplaceEnter(HtmlCode);
* string Reg="<title>.+?</title>";
* string GetValue=o.GetRegValue(Reg,HtmlCode)
* Response.Write(GetValue);
* o.Dispose();
* ********************************/
/**/
/// <summary>
/// 替换HTML源代码
/// </summary>
/// <param name="HtmlCode">html源代码</param>
/// <returns></returns>
public static string RemoveHTML(string HtmlCode)
{
string MatchVale = HtmlCode;
foreach (Match s in Regex.Matches(HtmlCode, "<.+?>"))
{
MatchVale = MatchVale.Replace(s.Value, "");
}
return MatchVale;
}
#endregion 替换HTML源代码
#region 匹配页面的链接
/**/
/**********************************
* 函数名称:GetHref
* 功能说明:匹配页面的链接
* 参 数:HtmlCode:html源代码
* 调用示例:
* GetRemoteObj o = new GetRemoteObj();
* string Url = @"http://www.baidu.com";
* string HtmlCode = o.GetRemoteHtmlCode(Url);
* string s = o.GetHref(HtmlCode);
* Response.Write(s);
* o.Dispose();
* ********************************/
/**/
/// <summary>
/// 获取页面的链接正则
/// </summary>
/// <param name="HtmlCode"></param>
/// <returns></returns>
public static string GetHref(string HtmlCode)
{
string MatchVale = "";
string Reg = @"(h|H)(r|R)(e|E)(f|F) *= *(’|"")?((\w|\\|\/|\.|:|-|_)+)[\S]*";
foreach (Match m in Regex.Matches(HtmlCode, Reg))
{
MatchVale += (m.Value).ToLower().Replace("href=", "").Trim() + "|";
}
return MatchVale;
}
#endregion 匹配页面的链接
#region 匹配页面的图片地址
/**/
/**********************************
* 函数名称:GetImgSrc
* 功能说明:匹配页面的图片地址
* 参 数:HtmlCode:html源代码;imgHttp:要补充的http.当比如:<img src="bb/x.gif">则要补充http://www.baidu.com/,当包含http信息时,则可以为空
* 调用示例:
* GetRemoteObj o = new GetRemoteObj();
* string Url = @"http://www.baidu.com";
* string HtmlCode = o.GetRemoteHtmlCode(Url);
* string s = o.GetImgSrc(HtmlCode,"http://www.baidu.com/");
* Response.Write(s);
* o.Dispose();
* ********************************/
/**/
/// <summary>
/// 匹配页面的图片地址
/// </summary>
/// <param name="HtmlCode"></param>
/// <param name="imgHttp">要补充的http://路径信息</param>
/// <returns></returns>
public static string GetImgSrc(string HtmlCode, string imgHttp)
{
string MatchVale = "";
string Reg = @"<img.+?>";
foreach (Match m in Regex.Matches(HtmlCode.ToLower(), Reg))
{
MatchVale += GetImg((m.Value).ToLower().Trim(), imgHttp) + "|";
}
return MatchVale;
}
/**/
/// <summary>
/// 匹配<img src="" />中的图片路径实际链接
/// </summary>
/// <param name="ImgString"><img src="" />字符串</param>
/// <returns></returns>
public static string GetImg(string ImgString, string imgHttp)
{
string MatchVale = "";
string Reg = @"src=.+\.(bmp|jpg|gif|png|)";
foreach (Match m in Regex.Matches(ImgString.ToLower(), Reg))
{
MatchVale += (m.Value).ToLower().Trim().Replace("src=", "");
}
if (MatchVale.IndexOf(".net") != -1 || MatchVale.IndexOf(".com") != -1 || MatchVale.IndexOf(".org") != -1 || MatchVale.IndexOf(".cn") != -1 || MatchVale.IndexOf(".cc") != -1 || MatchVale.IndexOf(".info") != -1 || MatchVale.IndexOf(".biz") != -1 || MatchVale.IndexOf(".tv") != -1)
return (MatchVale);
else
return (imgHttp + MatchVale);
}
#endregion 匹配页面的图片地址
#region 替换通过正则获取字符串所带的正则首尾匹配字符串
/**/
/**********************************
* 函数名称:GetHref
* 功能说明:匹配页面的链接
* 参 数:HtmlCode:html源代码
* 调用示例:
* GetRemoteObj o = new GetRemoteObj();
* string Url = @"http://www.baidu.com";
* string HtmlCode = o.GetRemoteHtmlCode(Url);
* string s = o.RegReplace(HtmlCode,"<title>","</title>");
* Response.Write(s);
* o.Dispose();
* ********************************/
/**/
/// <summary>
/// 替换通过正则获取字符串所带的正则首尾匹配字符串
/// </summary>
/// <param name="RegValue">要替换的值</param>
/// <param name="regStart">正则匹配的首字符串</param>
/// <param name="regEnd">正则匹配的尾字符串</param>
/// <returns></returns>
public static string RegReplace(string RegValue, string regStart, string regEnd)
{
string s = RegValue;
if (RegValue != "" && RegValue != null)
{
if (regStart != "" && regStart != null)
{
s = s.Replace(regStart, "");
}
if (regEnd != "" && regEnd != null)
{
s = s.Replace(regEnd, "");
}
}
return s;
}
#endregion 替换通过正则获取字符串所带的正则首尾匹配字符串
/// <summary>
/// 写文件开始
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static bool WriteStart(string htmlPathAndName, string htmls)
{
StreamWriter sw = null;
string htmlPathAndName1 = "";
//写文件
try
{
htmlPathAndName1 = HttpContext.Current.Server.MapPath(htmlPathAndName);
sw = new StreamWriter(htmlPathAndName1, false, Encoding.Default);
sw.Write(htmls);
sw.Flush();
//这里必须关闭,否则内存占用增长....
sw.Close();
return true;
}
catch (Exception ex)
{
//这里必须关闭,否则内存占用增长....
if (sw != null) sw.Close();
throw new Exception(htmlPathAndName1 + ex.Message);
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
return false;
}
finally
{
//这里必须关闭,否则内存占用增长....
if(sw!=null)sw.Close();
}
}
/// <summary>
/// 生成写文件
/// </summary>
/// <param name="strTopic"></param>
/// <param name="strArticleClass"></param>
/// <param name="strArticleClassChild"></param>
/// <param name="strContent"></param>
/// <param name="strSource"></param>
/// <param name="strAuthor"></param>
/// <param name="strEditor"></param>
/// <param name="strPubDate"></param>
/// <param name="strPageUrl"></param>
/// <returns></returns>
public static bool WriteFile(string strTopic, string strArticleClass, string strArticleClassChild, string strContent, string strSource, string strAuthor, string strEditor, string strPubDate, string strPageUrl)
{
/*
Encoding code = Encoding.GetEncoding("gb2312");
//读取模板文件
string temp = HttpContext.Current.Server.MapPath("/bluedn/template/NewsInfo.htm");
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); //读取文件
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename = strPageUrl + ".htm";
//DateTime.Now.ToString("yyyyMMddHHmmss")+".htm";
//替换内容
//这时,模板文件已经读入到名称为str的变量中了
//模板页中的新闻标题
str = str.Replace("$t_Topic$", strTopic);
//一级栏目
str = str.Replace("$t_ArticleClass$", strArticleClass);
//二级栏目
str = str.Replace("$t_ArticleClassChild$", strArticleClassChild);
//新闻来源
str = str.Replace("$t_Source$", strSource);
//作者
str = str.Replace("$t_Author$", strAuthor);
//编辑
str = str.Replace("$t_Editor$", strEditor);
//发布日期
str = str.Replace("$t_PubDate$", strPubDate);
//新闻内容
str = str.Replace("$t_Content$", strContent);
*/
return true;
}
}
}
![](http://www.cnblogs.com/images/cnblogs_com/920724025/日草.gif)