发布:2023/1/14 22:14:58作者:管理员 来源:本站 浏览次数:519
需求: 使用定时器定时检测一个网址是否可以打开。
代码:
using System;
using System.Net;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
private Timer TestTimer = new Timer();
public Form1()
{
InitializeComponent();
TestTimer.Tick += new EventHandler(Call);
TestTimer.Interval = 1000 * 10;
TestTimer.Enabled = true;
}
private void Call(object sender, EventArgs e)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.csdn.net/");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
MessageBox.Show("网址正常运行");
if (response.StatusCode != HttpStatusCode.OK)
{
MessageBox.Show("网址运行异常");
}
response.Close();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
}
}
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4