You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.5 KiB

2 years ago
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Web;
  6. namespace EasyBL.Handler
  7. {
  8. /// <summary>
  9. /// Crawler 的摘要说明
  10. /// </summary>
  11. public class CrawlerHandler : Handler
  12. {
  13. private string[] Sources;
  14. private Crawler[] Crawlers;
  15. public CrawlerHandler(HttpContext context) : base(context)
  16. {
  17. }
  18. public override void Process()
  19. {
  20. Sources = Request.Form.GetValues("source[]");
  21. if (Sources == null || Sources.Length == 0)
  22. {
  23. WriteJson(new
  24. {
  25. state = "参数错误:没有指定抓取源"
  26. });
  27. return;
  28. }
  29. Crawlers = Sources.Select(x => new Crawler(x, Server).Fetch()).ToArray();
  30. WriteJson(new
  31. {
  32. state = "SUCCESS",
  33. list = Crawlers.Select(x => new
  34. {
  35. state = x.State,
  36. source = x.SourceUrl,
  37. url = x.ServerUrl
  38. })
  39. });
  40. }
  41. }
  42. public class Crawler
  43. {
  44. public string SourceUrl { get; set; }
  45. public string ServerUrl { get; set; }
  46. public string State { get; set; }
  47. private HttpServerUtility Server { get; set; }
  48. public Crawler(string sourceUrl, HttpServerUtility server)
  49. {
  50. this.SourceUrl = sourceUrl;
  51. this.Server = server;
  52. }
  53. public Crawler Fetch()
  54. {
  55. var request = HttpWebRequest.Create(this.SourceUrl) as HttpWebRequest;
  56. using (var response = request.GetResponse() as HttpWebResponse)
  57. {
  58. if (response.StatusCode != HttpStatusCode.OK)
  59. {
  60. State = "Url returns " + response.StatusCode + ", " + response.StatusDescription;
  61. return this;
  62. }
  63. if (response.ContentType.IndexOf("image") == -1)
  64. {
  65. State = "Url is not an image";
  66. return this;
  67. }
  68. ServerUrl = PathFormatter.Format(Path.GetFileName(this.SourceUrl), Config.GetString("catcherPathFormat"));
  69. var savePath = Server.MapPath(ServerUrl);
  70. if (!Directory.Exists(Path.GetDirectoryName(savePath)))
  71. {
  72. Directory.CreateDirectory(Path.GetDirectoryName(savePath));
  73. }
  74. try
  75. {
  76. var stream = response.GetResponseStream();
  77. using (var reader = new BinaryReader(stream))
  78. {
  79. byte[] bytes;
  80. using (var ms = new MemoryStream())
  81. {
  82. var buffer = new byte[4096];
  83. int count;
  84. while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
  85. {
  86. ms.Write(buffer, 0, count);
  87. }
  88. bytes = ms.ToArray();
  89. }
  90. File.WriteAllBytes(savePath, bytes);
  91. State = "SUCCESS";
  92. }
  93. }
  94. catch (Exception e)
  95. {
  96. State = "抓取错误:" + e.Message;
  97. }
  98. return this;
  99. }
  100. }
  101. }
  102. }