windows计划任务自动生成静态首页

indexplan.vbs: 脚本程序,调用ie组建执行生成首页的程序这里强调的是必须打开ie,要用到ie的组建,遨游好像不行。然后把这个脚本程序添加到windows计划任务里,设置好执行时间间隔,看你首页跟新的快慢了,自己把握。

Dim IE
Set IE = CreateObject("InternetExplorer.Application")
ie.navigate("http://localhost:80/createindex.asp")-这里在服务器换成你的域名指向你的生成文件
ie.visible=0
Set IE = Nothing
ASP版本
Server.ScriptTimeOut=9999999 

Function BytesToBstr(body,Cset)'防止乱码函数

	dim objstream

	set objstream = Server.CreateObject("adodb.stream")

	objstream.Type = 1

	objstream.Mode =3

	objstream.Open

	objstream.Write body

	objstream.Position = 0

	objstream.Type = 2

	objstream.Charset = Cset

	BytesToBstr = objstream.ReadText

	objstream.Close

	set objstream = Nothing

End Function



Function GetHTTPPage(Url)'获取Html代码函数

	err.clear

	On Error Resume Next

	dim http 

	set http=Server.createobject("Microsoft.XMLHTTP") 

	Http.open "GET",url,false 

	Http.send()

	if Http.readystate<>4 then

		exit function 

	end if 

	GetHTTPPage = bytesToBSTR(Http.responseBody,"GB2312")

	set http = Nothing

	If Err Then

	   response.write err.description

	   Response.Write "<br><br><p align='center'><font color='red'><b>无法抓取本页面信息!!!</b></font></p>"

	End If

End function

Url="http://www.xxxxx.com/index.asp"

strHtml=GetHTTPPage(Url)

filename=server.mappath("index.html")

if strHtml<>"" then'读取到首页源代码

    set fso = Server.CreateObject("scripting.FileSystemObject")

	if fso.FileExists(filename) then '静态首页存在,删除旧页面

	   fso.DeleteFile filename 

	   set fout = fso.CreateTextFile(filename,true,False)'重新生成

	   fout.write strHtml

	   fout.close

	   set fout=nothing

	   set fso=nothing

	 else

	   set fout = fso.CreateTextFile(filename,true,False)

	   fout.write strHtml

	   fout.close

	   set fout=nothing

	   set fso=nothing

	 end if 

end if
ASP.NET版本:
using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Text;

using System.Text.RegularExpressions;

using System.IO;

using System.Net;



public partial class CreatIndex : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        //index.html页面存在先删除旧页面,然后生成新页面

        if (File.Exists(@"E:/product/index.html"))

        {

            File.Delete(@"E:/product/index.html");

            string URL = "http://www.xxxx.com";

            string Content = GetHtmlContent(URL);

            CreateIndex(Content);

        }

        //index.html页面不存在直接生成

        else

        {

            string URL = "http://www.xxxx.com";

            string Content = GetHtmlContent(URL);

            CreateIndex(Content);

        }

    }

    //生成index.html页面

    protected void CreateIndex(string con)

    {

           string IndexPath = @"E:/product/index.html";  

           //StreamWriter sr = File.CreateText(@"E:/product/index.html");

            StreamWriter sr = new StreamWriter(IndexPath, true, System.Text.Encoding.GetEncoding("GB2312"));

            Response.Write("生成Index.html文件成功。生成时间:" + System.DateTime.Now);

            sr.WriteLine(con);

            sr.Close();

    }

    //读取网页源代码

    private string GetHtmlContent(string Url)

    {

        string strHtmlContent = "";

        try

        {

            //声明一个HttpWebRequest请求

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

            //连接超时时间

            request.Timeout = 500000;

            request.Headers.Set("Pragma", "no-cache");

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream streamHtmlCode = response.GetResponseStream();

            Encoding encoding = Encoding.GetEncoding("GB2312");

            StreamReader streamReader = new StreamReader(streamHtmlCode, encoding);

            strHtmlContent = streamReader.ReadToEnd();

        }

        catch

        {

            Response.Write("对不起出错了");

        }

        return strHtmlContent;

    }

}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值