人民医院API

WebHelper.cs 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. namespace RMYY_CallCenter_Api.Utility
  9. {
  10. public class WebHelper
  11. {
  12. #region UrlDecode(对Url进行解码)
  13. /// <summary>
  14. /// 对Url进行解码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码
  15. /// </summary>
  16. /// <param name="url">url</param>
  17. public static string UrlDecode(string url)
  18. {
  19. return HttpUtility.UrlDecode(url);
  20. }
  21. /// <summary>
  22. /// 对Url进行解码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码
  23. /// </summary>
  24. /// <param name="url">url</param>
  25. /// <param name="encoding">字符编码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码</param>
  26. public static string UrlDecode(string url, Encoding encoding)
  27. {
  28. return HttpUtility.UrlDecode(url, encoding);
  29. }
  30. #endregion
  31. #region 去除HTML标记
  32. /// <summary>
  33. /// 去除HTML标记
  34. /// </summary>
  35. /// <param name="NoHTML">包括HTML的源码 </param>
  36. /// <returns>已经去除后的文字</returns>
  37. public static string NoHtml(string Htmlstring)
  38. {
  39. //删除脚本
  40. Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
  41. //删除HTML
  42. Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
  43. Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
  44. Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
  45. Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
  46. Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
  47. Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
  48. Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
  49. Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
  50. Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
  51. Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
  52. Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
  53. Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
  54. Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
  55. Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
  56. Htmlstring = Regex.Replace(Htmlstring, @"&hellip;", "", RegexOptions.IgnoreCase);
  57. Htmlstring = Regex.Replace(Htmlstring, @"&mdash;", "", RegexOptions.IgnoreCase);
  58. Htmlstring = Regex.Replace(Htmlstring, @"&ldquo;", "", RegexOptions.IgnoreCase);
  59. Htmlstring.Replace("<", "");
  60. Htmlstring = Regex.Replace(Htmlstring, @"&rdquo;", "", RegexOptions.IgnoreCase);
  61. Htmlstring.Replace(">", "");
  62. Htmlstring.Replace("\r\n", "");
  63. Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
  64. return Htmlstring;
  65. }
  66. #endregion
  67. }
  68. }