您的位置:首页 > 运维架构 > 网站架构

做一个windows服务,这个服务用来定时访问一个网站[转贴]

2005-04-26 12:57 471 查看
1、新建一个"Windows服务"项目
2、然后在工具箱中“组件”栏拖一个"Timer"下来
3、双击自动产生下列事件:
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//这里编写自动访问网站代码
System.Net.WebClient wc=new System.Net.WebClient();
wc.DownloadData("http://www.csdn.net");

}
2、在自动产生的Service1.cs文件中找到
protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
//这里启动和设置Timer
timer1.Interval = 10000;//毫秒
timer1.Start();
}
funy:
PageUrl1 = "http://www.csdn.net";
WebRequest request1 = WebRequest.Create(PageUrl1);
WebResponse response1 = request1.GetResponse();
Stream resStream1 = response1.GetResponseStream();
StreamReader sr1 = new StreamReader(resStream1, System.Text.Encoding.GetEncoding("GB2312"));
indexhtml1 = sr1.ReadToEnd();
resStream1.Close();
sr1.Close();
string templatefile1;
templatefile1 = @"d:/index.htm";
StreamWriter sw1 = new StreamWriter(templatefile1,false,System.Text.Encoding.GetEncoding("GB2312"));
sw1.Write(indexhtml1);
sw1.Close();
把逻辑功能代码改成我上面写的就实现对页面的访问了吧,而且还把页面的源代码存入了一个html
我是拿这个做成了一个服务,每一分钟做一次,目的是提高网站的访问速度,:)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐