HttpJob.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Host.Common;
  2. using Host.IJobs;
  3. using Host.IJobs.Model;
  4. using Host.Model;
  5. using Newtonsoft.Json;
  6. using Quartz;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Net.Http;
  10. using System.Threading.Tasks;
  11. using System.Web;
  12. using Talk.Extensions;
  13. namespace Host
  14. {
  15. public class HttpJob : JobBase<LogUrlModel>, IJob
  16. {
  17. public HttpJob() : base(new LogUrlModel())
  18. { }
  19. public override async Task NextExecute(IJobExecutionContext context)
  20. {
  21. //获取相关参数
  22. var requestUrl = context.JobDetail.JobDataMap.GetString(Constant.REQUESTURL)?.Trim();
  23. requestUrl = requestUrl?.IndexOf("http") == 0 ? requestUrl : "http://" + requestUrl;
  24. var requestParameters = context.JobDetail.JobDataMap.GetString(Constant.REQUESTPARAMETERS);
  25. var headersString = context.JobDetail.JobDataMap.GetString(Constant.HEADERS);
  26. var headers = headersString != null ? JsonConvert.DeserializeObject<Dictionary<string, string>>(headersString?.Trim()) : null;
  27. var requestType = (RequestTypeEnum)int.Parse(context.JobDetail.JobDataMap.GetString(Constant.REQUESTTYPE));
  28. LogInfo.Url = requestUrl;
  29. LogInfo.RequestType = requestType.ToString();
  30. LogInfo.Parameters = requestParameters;
  31. HttpResponseMessage response = new HttpResponseMessage();
  32. var http = HttpHelper.Instance;
  33. switch (requestType)
  34. {
  35. case RequestTypeEnum.Get:
  36. response = await http.GetAsync(requestUrl, headers);
  37. break;
  38. case RequestTypeEnum.Post:
  39. response = await http.PostAsync(requestUrl, requestParameters, headers);
  40. break;
  41. case RequestTypeEnum.Put:
  42. response = await http.PutAsync(requestUrl, requestParameters, headers);
  43. break;
  44. case RequestTypeEnum.Delete:
  45. response = await http.DeleteAsync(requestUrl, headers);
  46. break;
  47. }
  48. var result = HttpUtility.HtmlEncode(await response.Content.ReadAsStringAsync());
  49. LogInfo.Result = $"<span class='result'>{result.MaxLeft(1000)}</span>";
  50. if (!response.IsSuccessStatusCode)
  51. {
  52. LogInfo.ErrorMsg = $"<span class='error'>{result.MaxLeft(3000)}</span>";
  53. await ErrorAsync(LogInfo.JobName, new Exception(result.MaxLeft(3000)), JsonConvert.SerializeObject(LogInfo), MailLevel);
  54. context.JobDetail.JobDataMap[Constant.EXCEPTION] = $"<div class='err-time'>{LogInfo.BeginTime}</div>{JsonConvert.SerializeObject(LogInfo)}";
  55. }
  56. else
  57. {
  58. try
  59. {
  60. //这里需要和请求方约定好返回结果约定为HttpResultModel模型
  61. var httpResult = JsonConvert.DeserializeObject<HttpResultModel>(HttpUtility.HtmlDecode(result));
  62. if (!httpResult.IsSuccess)
  63. {
  64. LogInfo.ErrorMsg = $"<span class='error'>{httpResult.ErrorMsg}</span>";
  65. await ErrorAsync(LogInfo.JobName, new Exception(httpResult.ErrorMsg), JsonConvert.SerializeObject(LogInfo), MailLevel);
  66. context.JobDetail.JobDataMap[Constant.EXCEPTION] = $"<div class='err-time'>{LogInfo.BeginTime}</div>{JsonConvert.SerializeObject(LogInfo)}";
  67. }
  68. else
  69. await InformationAsync(LogInfo.JobName, JsonConvert.SerializeObject(LogInfo), MailLevel);
  70. }
  71. catch (Exception)
  72. {
  73. await InformationAsync(LogInfo.JobName, JsonConvert.SerializeObject(LogInfo), MailLevel);
  74. }
  75. }
  76. }
  77. }
  78. }