using log4net; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Quartz; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.IO; namespace JobReqWebData { public class JobSendATDRequest : IJob { private ILog log = LogManager.GetLogger(typeof(JobSendATDRequest)); private const string CfgFileName = "requestjson.cfg"; private static string CfgFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, CfgFileName); public void Execute(IJobExecutionContext context) { try { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string sqlQuery = context.JobDetail.JobDataMap.GetString("QuerySql"); string reqUrl = context.JobDetail.JobDataMap.GetString("ReqUrl"); string customer = context.JobDetail.JobDataMap.GetString("Customer"); string password = context.JobDetail.JobDataMap.GetString("Password"); int reqTimeout = Convert.ToInt32(context.JobDetail.JobDataMap.GetString("RequestTimeout")); log.Debug($"连接字符串:{connStr}"); log.Debug($"查询SQL:{sqlQuery}"); log.Debug($"请求URL:{reqUrl}"); log.Debug($"用户:{customer}"); log.Debug($"密码:{password}"); log.Debug($"请求超时:{reqTimeout}"); SqlConnection conn = new SqlConnection(connStr); SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlQuery, conn); DataTable tQuery = new DataTable(); dataAdapter.Fill(tQuery); if (tQuery.Rows.Count == 0) { log.Debug("没有要发送的数据!"); return; } var json = DataTableToJsonHelper.ConvertToJson(tQuery); log.Debug($"jsonStr:{json.ToString(Formatting.None)}"); JObject reqObj = new JObject(); reqObj.Add("custname", new JValue(customer)); reqObj.Add("custpsw", new JValue(password)); reqObj.Add("bslist", json); log.Debug($"reqObj:{reqObj.ToString(Formatting.None)}"); File.WriteAllText(CfgFilePath, reqObj.ToString(Formatting.None)); //发送请求数据 string rtn = WebRequestHelper.DoPost(reqUrl, reqObj.ToString(Formatting.None), reqTimeout * 1000); log.Debug($"数据返回:{rtn}"); var objRtn = JsonConvert.DeserializeAnonymousType(rtn, new { status = ""}); log.Debug($"爬取服务返回:(Code:{objRtn.status})"); } catch (Exception ex) { log.Error(ex.Message); log.Error(ex.StackTrace); } } } }