1.在应用程序池中右点击 ,选择固定时间间隔,设置为0,
2.如果还不行就增加一条,每2分钟访问一次本网站的记录
3.建议增加一个后台服务,定时激活任务
启动任务、停止任务代码
public async Task<Model.ret> Start()
{
Model.ret m = new Model.ret();
string u = get_session();
u = "1";
if (u == "")
{
m.Code = "0";
m.Msg = "开启失败!";
}
else
{
await ClearJobTrigger();
//从工厂中获取一个调度器实例化
scheduler = await StdSchedulerFactory.GetDefaultScheduler();
await scheduler.Start();
var list = getAll();
//创建一个作业
for (int i = 0; i < list.Count; i++)
{
try
{
IJobDetail job1 = JobBuilder.Create<Common.WebSite>()
.SetJobData(new JobDataMap() {
new KeyValuePair<string, object>("Url", list[i].Url),
new KeyValuePair<string, object>("Task_id", list[i].Task_id)
})
.WithIdentity(list[i].JobName, list[i].GropName)
.Build();
var trigger = TriggerBuilder.Create()
.WithCronSchedule(list[i].Cron)
.Build();
await scheduler.ScheduleJob(job1, trigger); //把作业,触发器加入调度器。
}
catch (Exception ex)
{
}
}
// 清除任务和触发器
//ClearJobTrigger();
m.Code = "1";
m.Msg = "开启成功!";
}
return m;
}
private List<jobs> getAll()
{
var list = new List<jobs>();
List<Model.Tasks> list1 = Common.freeSqlHelper.fsqlDb.Select<Model.Tasks>().Where(a => a.IsUse).ToList();
for (int i = 0; i < list1.Count; i++)
{
string url = list1[i].Url == null ? "" : list1[i].Url.Trim();
string Cron = list1[i].Cron == null ? "" : list1[i].Cron.Trim();
if (url == "" || Cron == "")
{
}
else
{
Models.jobs m = new jobs();
m.Url = url;
m.Cron = Cron;
m.JobName = "q" + list1[i].ID;
m.GropName = "DemoGroup";
m.Task_id = list1[i].ID.ToString();
list.Add(m);
}
}
return list;
}
WebSite类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz;
namespace Common
{
public class WebSite : IJob
{
public string Url { get; set; } = "";
public string Task_id { get; set; } = "";
//public WebSite(string url1)
//{
// url= url1;
//}
public Task Execute(IJobExecutionContext context)
{
int task_id1 = 0;
int.TryParse(Task_id,out task_id1);
//return Task.Factory.StartNew(() =>
//{
// Console.WriteLine($"Hello {Url}!");
//});
return Task.Factory.StartNew(() =>
{
if (Url != "")
{
var s= UrlPost.HttpGet1(Url);
if (s.Code == "1")
{
Common.Class1.setLog("0", "任务调度程序", "成功", "任务id为"+task_id1 + "," + s.Msg);
var t5 = freeSqlHelper.fsqlDb.Update<Model.Tasks>()
.Set(a => new Model.Tasks { LastTime=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),LastTime_note=s.Msg })//多个字段信息修改
.Where(a => a.ID==task_id1).ExecuteAffrows();
}
else
{
Common.Class1.setLog("0", "任务调度程序", "失败", "任务id为"+task_id1 + "," + s.Msg);
}
}
context.JobDetail.JobDataMap.Put("RunSuccess", true);
});
}
}
}
UrlPost类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
namespace Common
{
public class UrlPost
{
//static object lock_url = new object();
/// <summary>
/// 模拟打开网址 get方式
/// </summary>
/// <param name="url">网址</param>
/// <param name="senddata">json参数</param>
/// <returns></returns>
static public Model.ret HttpGet(string url, string senddata)
{
Model.ret m = new Model.ret();
//lock (lock_url)
//{
string r1 = "";
try
{
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
System.Net.WebClient webc = new System.Net.WebClient();
var apiurl = new Uri(url);
string sendstr = senddata;
webc.Headers.Add("Content-Type", "text/xml");
//webc.Headers["Content-Type"] = "application/stream;charset=utf-8";//OK
var arr = webc.UploadData(apiurl, Encoding.UTF8.GetBytes(sendstr));
r1 = Encoding.UTF8.GetString(arr);
m.Code = "1";
m.Msg = "访问成功!";
}
catch (Exception ex)
{
m.Code = "1";
m.Msg = "访问失败,原因:" + ex.Message;
}
return m;
//}
}
/// <summary>
/// 模拟打开网址
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
static public Model.ret HttpGet1(string url, string encoding = "UTF-8")
{
Model.ret m = new Model.ret();
string s3 = "";
//lock (lock_url)
//{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//声明一个HttpWebRequest请求
request.Timeout = 30000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
//request.Method = "POST";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
//StreamReader streamReader = new StreamReader(streamReceive, Encoding.GetEncoding("GB2312"));
//StreamReader streamReader = new StreamReader(streamReceive, Encoding.GetEncoding(encoding));
if (encoding == "Default")
{
StreamReader streamReader = new StreamReader(streamReceive, Encoding.Default);
s3 = streamReader.ReadToEnd();
}
else
{
StreamReader streamReader = new StreamReader(streamReceive, Encoding.GetEncoding(encoding));
s3 = streamReader.ReadToEnd();
}
m.Code = "1";
m.Msg = "访问成功!";
}
catch (Exception ex)
{
m.Code = "0";
m.Msg = "访问失败,原因:" + ex.Message;
}
return m;
//}
}
/// <summary>
/// 模拟打开网址 post方式
/// </summary>
/// <param name="Url"></param>
/// <param name="postDataStr"></param>
/// <returns></returns>
static public string HttpPost(string Url, string dataStr = "")
{
string retString = "";
//lock (lock_url)
//{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
request.ContentLength = Encoding.UTF8.GetByteCount(dataStr);
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(dataStr);
myStreamWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
}
catch (Exception ex)
{
}
//}
return retString;
}
static public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{ // 总是接受
return true;
}
}
}
namespace WebApplication1.Models
{
public class jobs
{
public string Task_id { set; get; } = "";
public string Url { set; get; } = "";
public string TiggerName { set; get; } = "";
public string JobName { set; get; } = "";
public string GropName { set; get; } = "";
public string Cron { set; get; } = "";
}
}