ScannerService.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Timers;
  11. using WebScanner.Tools;
  12. namespace WebScanner
  13. {
  14. public class ScannerService
  15. {
  16. private ScannerService()
  17. {
  18. //int msInterval = Convert.ToInt16(ConfigurationManager.AppSettings["TimeSpan"]);
  19. int msInterval = AppSetting.GetInt("TimeInterval");
  20. Timer timer = new Timer();
  21. timer.Elapsed += Timer_Elapsed;
  22. timer.Enabled = true;
  23. timer.Interval = msInterval * 1000;
  24. }
  25. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  26. {
  27. //var ms = ConfigurationManager.AppSettings["HostApi"].ToString();
  28. var ms = AppSetting.GetValue("HostApi");
  29. var webs = ms.Split(';');
  30. foreach (var web in webs)
  31. {
  32. Action ac = () => HttpRequest(web, contenttype: "text/html; charset=utf-8");
  33. ac.Invoke();
  34. }
  35. }
  36. /// <summary>
  37. /// 不做catch处理,需要在外部做
  38. /// </summary>
  39. /// <param name="url"></param>
  40. /// <param name="method">默认GET,空则补充为GET</param>
  41. /// <param name="contenttype">默认json,空则补充为json</param>
  42. /// <param name="header">请求头部</param>
  43. /// <param name="data">请求body内容</param>
  44. /// <returns></returns>
  45. public static string HttpRequest(string url, string method = "GET", string contenttype = "application/json;charset=utf-8", Hashtable header = null, string data = null)
  46. {
  47. typeof(ScannerService).LogInfo($"request start:{url}");
  48. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  49. request.Method = string.IsNullOrEmpty(method) ? "GET" : method;
  50. request.ContentType = string.IsNullOrEmpty(contenttype) ? "application/json;charset=utf-8" : contenttype;
  51. request.Timeout = 30000;
  52. if (header != null)
  53. {
  54. foreach (var i in header.Keys)
  55. {
  56. request.Headers.Add(i.ToString(), header[i].ToString());
  57. }
  58. }
  59. if (!string.IsNullOrEmpty(data))
  60. {
  61. Stream requestStream = request.GetRequestStream();
  62. byte[] bytes = Encoding.UTF8.GetBytes(data);
  63. requestStream.Write(bytes, 0, bytes.Length);
  64. requestStream.Close();
  65. }
  66. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  67. Stream responseStream = response.GetResponseStream();
  68. StreamReader streamReader = new StreamReader(responseStream ?? throw new InvalidOperationException(), Encoding.GetEncoding("utf-8"));
  69. string re = streamReader.ReadToEnd();
  70. streamReader.Close();
  71. responseStream.Close();
  72. typeof(ScannerService).LogInfo($"request end:{re.Substring(0,100)}");
  73. return re;
  74. }
  75. private static volatile ScannerService _scannerService = null;
  76. public static ScannerService GetSingleObj()
  77. {
  78. return _scannerService ?? (_scannerService = new ScannerService());
  79. }
  80. }
  81. }