CommonMethod.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using FCS.Common;
  2. using HtmlAgilityPack;
  3. using System;
  4. using System.IO;
  5. using System.Net;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. namespace FCS.Crawler.Tools
  9. {
  10. public class CommonMethod
  11. {
  12. #region 获取全局唯一GUID
  13. /// <summary>
  14. /// 获取全局唯一GUID
  15. /// </summary>
  16. /// <param name="needReplace">是否需要替换-</param>
  17. /// <param name="format">格式化</param>
  18. /// <example>N:38bddf48f43c48588e0d78761eaa1ce6</example>>
  19. /// <example>P:(778406c2-efff-4262-ab03-70a77d09c2b5)</example>>
  20. /// <example>B:{09f140d5-af72-44ba-a763-c861304b46f8}</example>>
  21. /// <example>D:57d99d89-caab-482a-a0e9-a0a803eed3ba</example>>
  22. /// <returns></returns>
  23. public static string GetGuid(bool needReplace = true, string format = "N")
  24. {
  25. Guid res = NewSequentialGuid();//Guid.NewGuid();
  26. return needReplace ? res.ToString(format) : res.ToString();
  27. }
  28. [System.Runtime.InteropServices.DllImport("rpcrt4.dll", SetLastError = true)]
  29. private static extern int UuidCreateSequential(byte[] buffer);
  30. internal static object GetUrl(string Url)
  31. {
  32. string js = "";
  33. HtmlNode node = null;
  34. var htmlResource = NetHelper.GetUrlResponse(Url, Encoding.GetEncoding("utf-8"));
  35. js = GetJs(htmlResource);
  36. return js;
  37. }
  38. /// <summary>
  39. /// 创建有序GUID
  40. /// </summary>
  41. /// <returns></returns>
  42. private static Guid NewSequentialGuid()
  43. {
  44. byte[] raw = new byte[16];
  45. if (UuidCreateSequential(raw) != 0)
  46. throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
  47. byte[] fix = new byte[16];
  48. // 反转 0..3
  49. fix[0x0] = raw[0x3];
  50. fix[0x1] = raw[0x2];
  51. fix[0x2] = raw[0x1];
  52. fix[0x3] = raw[0x0];
  53. // 反转 4 & 5
  54. fix[0x4] = raw[0x5];
  55. fix[0x5] = raw[0x4];
  56. // 反转 6 & 7
  57. fix[0x6] = raw[0x7];
  58. fix[0x7] = raw[0x6];
  59. // 后8位不做操作,实际为当前MAC地址
  60. fix[0x8] = raw[0x8];
  61. fix[0x9] = raw[0x9];
  62. fix[0xA] = raw[0xA];
  63. fix[0xB] = raw[0xB];
  64. fix[0xC] = raw[0xC];
  65. fix[0xD] = raw[0xD];
  66. fix[0xE] = raw[0xE];
  67. fix[0xF] = raw[0xF];
  68. return new Guid(fix);
  69. }
  70. #endregion 获取全局唯一GUID
  71. #region 常用的html获取js
  72. /// <summary>
  73. /// 根据htmL获取Js
  74. /// </summary>
  75. /// <param name="Url"></param>
  76. /// <returns></returns>
  77. public static string GetUrl()
  78. {
  79. string js = "";
  80. HtmlNode node = null;
  81. var htmlResource = NetHelper.GetUrlResponse("", Encoding.GetEncoding("utf-8"));
  82. js = GetJs(htmlResource);
  83. return js;
  84. }
  85. public static string GetJs(string html)
  86. {
  87. string js1 = "";
  88. //正则表达式获取jsData后面的js
  89. Regex r1 = new Regex(@"jsData/(\w|\W)*?"">");
  90. MatchCollection m1 = r1.Matches(html);
  91. foreach (Match item in m1)
  92. {
  93. js1 = item.Value.Replace("\">", "");
  94. }
  95. if (string.IsNullOrEmpty((js1)))
  96. {
  97. return "";
  98. }
  99. else
  100. {
  101. return js1;
  102. }
  103. }
  104. #endregion 常用的html获取js
  105. #region 根据htmL获取Js
  106. public static string HttpJs(string url)
  107. {
  108. WebRequest req = WebRequest.Create(url);
  109. WebResponse resp = req.GetResponse();
  110. StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
  111. string html = reader.ReadToEnd();
  112. return html;
  113. }
  114. #endregion 根据htmL获取Js
  115. }
  116. }