IDE:vs2019
项目文件:
链接: https://pan.baidu.com/s/1H-Q3nc6sKQbD32pp0vuQIg
提取码: ncce
使用场景:定时访问本机(服务器)的某个页面,发送参数。执行计划任务。
界面:
C# 代码:
using System; using System.Net; using System.Text; using System.Windows.Forms; using System.Web; using System.IO; using System.Text.RegularExpressions;namespace CronVisitURL {public partial class FormMain : Form{// 初始化-参数public int autoStart = 1; // 自动开始标记public int setLines = 5; // 配置信息行数public string strURL = ""; // 访问urlpublic string strPostKey = ""; // 访问postKeypublic string strPostData = ""; // 访问postDatepublic int startTime = 0; // 运行时段-起始public int endTime = 0; // 运行时段-结束public Regex regex = new Regex(@"^http:\/\/(127\.0\.0\.1)|(localhost)\/"); // 访问url,校验规则public string iniPath = Application.StartupPath + @"\setting.ini"; // 配置文件路径public FormMain(){InitializeComponent();}// 窗体载入后private void FormMain_Load(object sender, EventArgs e){//读取配置信息 SetInfoRead();}// 状态栏图标单击private void NotifyIcon1_MouseClick(object sender, MouseEventArgs e){// 左键单击if (e.Button == MouseButtons.Left){Show();Activate();WindowState = FormWindowState.Normal;}}// 状态栏图标菜单-退出private void ExitMenuItem_Click(object sender, EventArgs e){if (MessageBox.Show("你确定要退出程序吗?", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK){notifyIcon1.Visible = false;Close();Dispose();Application.Exit();}}//状态栏图标菜单-显示private void ShowMenuItem_Click(object sender, EventArgs e){Show();Activate();WindowState = FormWindowState.Normal;}// 按钮-默认private void BtnDefault_Click(object sender, EventArgs e){TxbUrl.Text = "http://127.0.0.1/mws/cron/cron.php";TxbPostKey.Text = "hf_tech";TxbPostData.Text = "good";NumStartHour.Value = 0;NumStartMinute.Value = 0;NumStartSecond.Value = 0;NumEndHour.Value = 23;NumEndMinute.Value = 59;NumEndSecond.Value = 59;NumVisitRate.Value = 1;autoStart = 1;LabMsg.Text = "恢复成功";}// 按钮-保存private void BtnSave_Click(object sender, EventArgs e){SetInfoSave();}// 按钮-退出private void BtnExit_Click(object sender, EventArgs e){DialogResult result = MessageBox.Show("是否退出?选否,最小化到托盘", "操作提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);if (result == DialogResult.Yes){Close();Dispose();Application.Exit();}else if(result == DialogResult.No){WindowState = FormWindowState.Minimized;Visible = false;notifyIcon1.Visible = true;}}// 按钮-隐藏private void BtnHide_Click(object sender, EventArgs e){Hide();notifyIcon1.Visible = true;}// 按钮-暂停private void BtnPause_Click(object sender, EventArgs e){// 修改界面BtnPause.Enabled = false;BtnStart.Enabled = true;groupBox1.Enabled = true;groupBox2.Enabled = true;panel1.Enabled = true;LabMsg.Text = "定时访问,暂停";timer1.Stop();}// 按钮-开始private void BtnStart_Click(object sender, EventArgs e){// 初始化int visitRate = Convert.ToInt32(NumVisitRate.Value.ToString());strURL = TxbUrl.Text.Trim(); // urlstrPostKey = TxbPostKey.Text.Trim(); // PostKeystrPostData = TxbPostData.Text.Trim(); // PostData// 校验urlif (regex.IsMatch(strURL) == false){throw new SetException("配置信息异常:访问url,必须以本地域名或本机IP开头");}// 校验postif (strPostKey == ""){throw new SetException("访问postKey,数值异常");}if (strPostData == ""){throw new SetException("访问postData,数值异常");}// 校验频率if (visitRate > 3600 || visitRate < 1){throw new SetException("访问频率,数值异常");}// 获取运行时段 GetRunTime();// 开始访问 StartVist(visitRate);}// 开始访问private void StartVist(int visitRate){// 修改界面BtnPause.Enabled = true;BtnStart.Enabled = false;groupBox1.Enabled = false;groupBox2.Enabled = false;panel1.Enabled = false;LabMsg.Text = "定时访问,开始";// 启动计时器timer1.Interval = visitRate * 1000; // 默认毫秒 timer1.Start();}// 计时器private void Timer1_Tick(object sender, EventArgs e){// 获取当前时间string timeStr = DateTime.Now.ToLongTimeString().ToString();string result = Regex.Replace(timeStr, @"[^0-9]+", "");int timeNow = Convert.ToInt32(result);// 如果在运行时段内if (startTime <= timeNow && timeNow <= endTime){try{VisitURL();}catch{LabMsg.Text = "访问失败,请检查后,重试";timer1.Stop();}}}// 绑定当前时间是否在运行时段内private void GetRunTime(){// 获取运行时段string statrHour = NumStartHour.Value.ToString(); // 开始-时string statrMinute = NumStartMinute.Value.ToString(); // 开始-分string statrSecond = NumStartSecond.Value.ToString(); // 开始-秒string endHour = NumEndHour.Value.ToString(); // 结束-时string endMinute = NumEndMinute.Value.ToString(); // 结束-分string endSecond = NumEndSecond.Value.ToString(); // 结束-秒// 补零if (Convert.ToInt32(statrMinute) < 10){statrMinute = "0" + statrMinute;}if (Convert.ToInt32(statrSecond) < 10){statrSecond = "0" + statrSecond;}if (Convert.ToInt32(endMinute) < 10){endMinute = "0" + endMinute;}if (Convert.ToInt32(endSecond) < 10){endSecond = "0" + endSecond;}// 转换string startStr = statrHour + statrMinute + statrSecond;string endStr = endHour + endMinute + endSecond;startTime = Convert.ToInt32(startStr);endTime = Convert.ToInt32(endStr);}// 访问url-postprivate void VisitURL(){// 初始化HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);//Post请求方式request.Method = "POST";// 内容类型request.ContentType = "application/x-www-form-urlencoded";// 参数经过URL编码string paraUrlCoded = HttpUtility.UrlEncode(strPostKey);paraUrlCoded += "=" + HttpUtility.UrlEncode(strPostData);byte[] payload;//将URL编码后的字符串转化为字节payload = Encoding.UTF8.GetBytes(paraUrlCoded);//设置请求的 ContentLength request.ContentLength = payload.Length;//获得请 求流Stream writer = request.GetRequestStream();//将请求参数写入流writer.Write(payload, 0, payload.Length);// 关闭请求流 writer.Close();// 获得响应流HttpWebResponse response = (HttpWebResponse)request.GetResponse();StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);string responseText = reader.ReadToEnd();reader.Close();LabMsg.Text = DateTime.Now.ToLongTimeString().ToString() + ",收到数据:" + responseText;}// 自动开始-图片private void PicAutoStart_Click(object sender, EventArgs e){// 不打勾if (autoStart == 1){PicAutoStart.Image = Properties.Resources.cb1;autoStart = 0;}// 打勾else{PicAutoStart.Image = Properties.Resources.cb2;autoStart = 1;}}// 自动开始-标签private void LabAutoStart_Click(object sender, EventArgs e){// 不打勾if (autoStart == 1){PicAutoStart.Image = Properties.Resources.cb1;autoStart = 0;}// 打勾else{PicAutoStart.Image = Properties.Resources.cb2;autoStart = 1;}}// 配置信息-读取private void SetInfoRead(){if (File.Exists(iniPath)){try{// 初始化-读取数据string[] lines = File.ReadAllLines(iniPath, Encoding.UTF8); // 将文本内容转换为数组string[] line1 = lines[1].Split(','); // postKey,Postdatastring[] line2 = lines[2].Split(','); // 开始时,分,秒string[] line3 = lines[3].Split(','); // 结束时,分,秒// 初始化-数据转换string[] visit = new string[3];int[] cron = new int[8];visit[0] = lines[0]; // 访问urlvisit[1] = line1[0]; // 访问postKeyvisit[2] = line1[1]; // 访问postDatacron[0] = Convert.ToInt32(line2[0]); // 开始-时cron[1] = Convert.ToInt32(line2[1]); // 开始-分cron[2] = Convert.ToInt32(line2[2]); // 开始-秒cron[3] = Convert.ToInt32(line3[0]); // 结束-时cron[4] = Convert.ToInt32(line3[1]); // 结束-分cron[5] = Convert.ToInt32(line3[2]); // 结束-秒cron[6] = Convert.ToInt32(lines[4]); // 访问频率cron[7] = Convert.ToInt32(lines[5]); // 自动开始// 初始化-数据校验bool result = SetInfoCheck(visit, cron);if(result == true){// 赋值TxbUrl.Text = visit[0];TxbPostKey.Text = visit[1];TxbPostData.Text = visit[2];NumStartHour.Value = cron[0];NumStartMinute.Value = cron[1];NumStartSecond.Value = cron[2];NumEndHour.Value = cron[3];NumEndMinute.Value = cron[4];NumEndSecond.Value = cron[5];NumVisitRate.Value = cron[6];autoStart = cron[7];// 开启后自动运行if (autoStart == 1){//打勾PicAutoStart.Image = Properties.Resources.cb2;// 初始化strURL = visit[0];strPostKey = visit[1];strPostData = visit[2];// 获取运行时段 GetRunTime();// 自动开始StartVist(cron[6]);}else{// 不打勾PicAutoStart.Image = Properties.Resources.cb1;}LabMsg.Text = "配置信息,读取成功";}}catch{LabMsg.Text = "配置信息,读取失败,请重新保存设置";}}else{LabMsg.Text = "配置文件,未找到,请重新保存设置";}}// 配置信息-保存private void SetInfoSave(){// 初始化string[] visit = new string[3];int[] cron = new int[8];visit[0] = TxbUrl.Text.Trim(); // 访问urlvisit[1] = TxbPostKey.Text.Trim(); // 访问postKeyvisit[2] = TxbPostData.Text.Trim(); // 访问postDatacron[0] = Convert.ToInt32(NumStartHour.Value.ToString()); // 开始-时cron[1] = Convert.ToInt32(NumStartMinute.Value.ToString()); // 开始-分cron[2] = Convert.ToInt32(NumStartSecond.Value.ToString()); // 开始-秒cron[3] = Convert.ToInt32(NumEndHour.Value.ToString()); // 结束-时cron[4] = Convert.ToInt32(NumEndMinute.Value.ToString()); // 结束-分cron[5] = Convert.ToInt32(NumEndSecond.Value.ToString()); // 结束-秒cron[6] = Convert.ToInt32(NumVisitRate.Value.ToString()); // 访问频率cron[7] = autoStart; // 自动开始// 数据校验bool result = SetInfoCheck(visit, cron);if (result == true){// 设置信息-字符串string str = visit[0] + Environment.NewLine;str += visit[1] + ',' + visit[2] + Environment.NewLine;str += cron[0].ToString() + ',' + cron[1].ToString() + ',' + cron[2].ToString() + Environment.NewLine;str += cron[3].ToString() + ',' + cron[4].ToString() + ',' + cron[5].ToString() + Environment.NewLine;str += cron[6].ToString() + Environment.NewLine;str += cron[7].ToString();// 写入配置文件 File.WriteAllText(iniPath, str, Encoding.UTF8);LabMsg.Text = "配置信息,保存成功";}}// 配置数据校验private bool SetInfoCheck(string[] visit, int[] cron){// 参数个数-访问if(visit.GetLength(0) !=3){LabMsg.Text = "访问参数,数值异常";return false;}// 参数个数-每日运行时段,访问频率,自动运行if (cron.GetLength(0) !=8){LabMsg.Text = "计时器,数值异常";return false;}// 访问-urlif (regex.IsMatch(visit[0]) == false){LabMsg.Text = "配置信息异常:访问url,必须以本地域名或本机IP开头";return false;}// 访问-postKeyif (visit[1] == ""){LabMsg.Text = "访问postKey,数值异常";return false;}// 访问-postDataif (visit[2] == ""){LabMsg.Text = "访问postData,数值异常";return false;}// 开始-时if (cron[0] > 23 || cron[0] < 0){LabMsg.Text = "每日运行时段,开始小时,数值异常";return false;}// 开始-分if (cron[1] > 60 || cron[1] < 0){LabMsg.Text = "每日运行时段,开始分钟,数值异常";return false;}// 开始-秒if (cron[2] > 60 || cron[2] < 0){LabMsg.Text = "每日运行时段,开始秒钟,数值异常";return false;}// 结束-时if (cron[3] > 23 || cron[3] < 0){LabMsg.Text = "每日运行时段,结束小时,数值异常";return false;}// 结束-分if (cron[4] > 60 || cron[4] < 0){LabMsg.Text = "每日运行时段,结束分钟,数值异常";return false;}// 结束-秒if (cron[5] > 60 || cron[5] < 0){LabMsg.Text = "每日运行时段,结束秒钟,数值异常";return false;}// 访问频率if (cron[6] > 3600 || cron[ 6] < 1){LabMsg.Text = "访问频率,数值异常";return false;}// 自动开始if (cron[7] > 1 || cron[7] < 0){LabMsg.Text = "自动运行,数值异常";return false;}return true;}}// 自定义异常类class SetException : Exception{public SetException(string message) : base(message){MessageBox.Show(message);}} }// the end @nb;
php 代码:
<?php // 本脚本所有的输出都会在触发器状态栏上显示,如果输入内容过多可能会导致触发器崩溃// 基本设置 ignore_user_abort(true); // 忽略客户端断开 set_time_limit(0); // 设置执行不超时 date_default_timezone_set('Asia/Shanghai'); // 设置时区// 定时访问 if(empty($_POST[POST_KEY]) || $_POST[POST_KEY] != POST_DATA) {die('error_post'); }//访问者id $remote_ip = $_SERVER["REMOTE_ADDR"];//如果是本机访问(ipv4|ipv6) if($remote_ip != '127.0.0.1' && $remote_ip != '::1') {die('error_ip'); }// do something// 输出反馈信息给触发器 echo $_POST[POST_KEY];// the end @nb;