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

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
我是拿这个做成了一个服务,每一分钟做一次,目的是提高网站的访问速度,:)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值