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.

127 lines
5.0 KiB

2 years ago
  1. using Microsoft.SqlServer.Server;
  2. using System;
  3. using System.Data.SqlTypes;
  4. using System.IO;
  5. using System.Net;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. namespace EasyBL
  9. {
  10. /// <summary>
  11. /// Author : John Date : 2018-04-31
  12. /// Description: 在SQL Server环境中执行的CLR方法,注意提供给SQL Server调用的方法必须有SqlFunction/SqlProcedure Attribute
  13. /// </summary>
  14. public sealed class SqlCLR
  15. {
  16. #region [函数]
  17. /// <param name="pattern">todo: describe pattern parameter on IsMatch</param>
  18. /// <param name="options">todo: describe options parameter on IsMatch</param>
  19. /// <param name="source">todo: describe source parameter on IsMatch</param>
  20. [SqlFunction(IsDeterministic = true)]
  21. public static SqlBoolean IsMatch(string source, string pattern, int options)
  22. {
  23. if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(pattern))
  24. {
  25. return SqlBoolean.False;
  26. }
  27. var regexOptions = RegexOptions.None;
  28. const int optionIgnoreCase = 1;
  29. const int optionMultiline = 2;
  30. if ((options & optionIgnoreCase) != 0)
  31. {
  32. regexOptions = regexOptions | RegexOptions.IgnoreCase;
  33. }
  34. if ((options & optionMultiline) != 0)
  35. {
  36. regexOptions = regexOptions | RegexOptions.Multiline;
  37. }
  38. return (SqlBoolean)(Regex.IsMatch(source, pattern, regexOptions));
  39. }
  40. /// <summary>
  41. /// 判断是否为中文
  42. /// </summary>
  43. /// <param name="source"></param>
  44. /// <returns></returns>
  45. [SqlFunction]
  46. public static SqlBoolean IsChinese(string source)
  47. {
  48. if (string.IsNullOrEmpty(source) || source.Trim() == string.Empty)
  49. {
  50. return false;
  51. }
  52. source = source.Trim();
  53. var r = System.Text.RegularExpressions.Regex.IsMatch(source, @"[\u4e00-\u9fa5]+$");
  54. return (SqlBoolean)r;
  55. }
  56. /// <summary>
  57. /// 根据url获取html
  58. /// </summary>
  59. /// <param name="url">url</param>
  60. /// <returns></returns>
  61. [SqlFunction(IsDeterministic = true)]
  62. public static string Fun_GetHTML(string url)
  63. {
  64. var html = string.Empty;
  65. html = GetAccess(url, "");
  66. return html;
  67. }
  68. /// <summary>
  69. /// "GET"
  70. /// </summary>
  71. /// <param name="url">web url</param>
  72. /// <param name="Referer">web referer</param>
  73. /// <returns>return the web access result</returns>
  74. public static string GetAccess(string url, string Referer)
  75. {
  76. try
  77. {
  78. var res = (HttpWebRequest)WebRequest.Create(url);
  79. var mycookiecontainer = new CookieContainer();
  80. res.CookieContainer = mycookiecontainer;
  81. res.Method = "GET";
  82. //res.Proxy = null;
  83. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  84. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
  85. //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback();
  86. res.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
  87. //res.Headers.Add("Accept-Encoding", "gzip, deflate");
  88. res.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
  89. //res.KeepAlive = false;
  90. res.ProtocolVersion = HttpVersion.Version11;
  91. if (Referer != "")
  92. {
  93. res.Referer = Referer;
  94. }
  95. res.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:30.0) Gecko/20100101 Firefox/30.0";
  96. using (HttpWebResponse resp = (HttpWebResponse)res.GetResponse())
  97. {
  98. //resp.Cookies = Form1.mycookiecontainer.GetCookies(res.RequestUri);
  99. using (Stream responseStream = resp.GetResponseStream())
  100. {
  101. //如果网页流压缩了,要加下面一句
  102. //responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
  103. using (StreamReader mySreamReader = new StreamReader(responseStream, Encoding.Default))//GB2312,utf-8,GBK
  104. {
  105. var responseData = mySreamReader.ReadToEnd();
  106. //MessageBox.Show(responseData);
  107. responseStream.Close();
  108. mySreamReader.Close();
  109. resp.Close();
  110. return responseData;
  111. }
  112. }
  113. }
  114. }
  115. catch (Exception ex)
  116. {
  117. return ex.ToString();
  118. }
  119. }
  120. #endregion [函数]
  121. }//end of class
  122. }