URLEncodeUtils.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Globalization;
  5. namespace COSXML.Utils
  6. {
  7. /// <summary>
  8. /// * URL Encode:
  9. /// * 可分为绝对不需要编码的字符:[a~z][A~A][0~9][._-~]
  10. /// * 特殊字符需要视情况而言:!*'();:@&amp;=+$,/?#[]
  11. /// * 空字符用+或者%20代替
  12. /// * 汉字绝地需要编码
  13. /// * 因此,针对完整URL则要分块进行编码:path块,query块,fragment块,host块
  14. /// * path块,需要以'/'进行分割,即 '/' 不需要编码,其它均需要编码
  15. /// * query块,需要以'&amp;'分割一对对key=value;key 和 value 需要编码, "="不编码
  16. /// * fragment,全部进行编码
  17. /// * URL Decode:
  18. /// * 不需要判断是否是特殊字符,因为其解码规则很简单,直接根据内容中是否出现%来判断是否需要解码,
  19. /// * 还是直接输出:若出现了%,则连续读出其后两位进行解码;反之,直接输出
  20. /// </summary>
  21. public sealed class URLEncodeUtils
  22. {
  23. //只有字母和数字[0-9a-zA-Z]、一些特殊符号"-_.!*~',以及某些保留字,才可以不经过编码直接用于URL。"
  24. public const string URLAllowChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
  25. /// <summary>
  26. /// 针对 URL 中 path 编码,则需要先将其按照 '/'分割,然后进行逐个块进行 value 编码
  27. /// </summary>
  28. /// <param name="path"></param>
  29. /// <returns></returns>
  30. public static string EncodePathOfURL(string path)
  31. {
  32. if (String.IsNullOrEmpty(path))
  33. {
  34. return String.Empty;
  35. }
  36. char separator = '/';
  37. int start = 0;
  38. int length = path.Length;
  39. int index = path.IndexOf(separator, start);
  40. StringBuilder result = new StringBuilder();
  41. while (index != -1 && start < length)
  42. {
  43. if (start != index)
  44. {
  45. result.Append(Encode(path.Substring(start, index - start)));
  46. }
  47. result.Append(separator);
  48. start = index + 1;
  49. index = path.IndexOf(separator, start);
  50. }
  51. if (start < length)
  52. {
  53. result.Append(Encode(path.Substring(start)));
  54. }
  55. return result.ToString();
  56. }
  57. public static string Encode(string value)
  58. {
  59. return Encode(value, Encoding.UTF8);
  60. }
  61. /// <summary>
  62. /// 针对单个value,则只需满足 URLAllowChars 不需要编码即可.
  63. /// urlEncode: 转为一个byte -> 转为两个16进制 -> 前面加上 %
  64. /// </summary>
  65. /// <param name="value"></param>
  66. /// <param name="encoding"></param>
  67. /// <returns></returns>
  68. public static string Encode(string value, Encoding encoding)
  69. {
  70. if (String.IsNullOrEmpty(value))
  71. {
  72. return String.Empty;
  73. }
  74. // %xy%xy
  75. // %xy%xy
  76. StringBuilder result = new StringBuilder(value.Length * 2);
  77. byte[] strToBytes = encoding.GetBytes(value);
  78. foreach (byte b in strToBytes)
  79. {
  80. char ch = (char)b;
  81. if (URLAllowChars.IndexOf(ch) != -1)
  82. {
  83. result.Append(ch);
  84. }
  85. else
  86. {
  87. result.Append('%').Append(String.Format(CultureInfo.InvariantCulture, "{0:X2}", (int)b));
  88. }
  89. }
  90. return result.ToString();
  91. }
  92. /// <summary>
  93. /// 解码比较统一,此处借用 Uri 来实现
  94. /// </summary>
  95. /// <param name="valueEncode"></param>
  96. /// <returns></returns>
  97. public static string Decode(string valueEncode)
  98. {
  99. if (String.IsNullOrEmpty(valueEncode))
  100. {
  101. return String.Empty;
  102. }
  103. valueEncode.Replace('+', ' ');
  104. return Uri.UnescapeDataString(valueEncode);
  105. }
  106. }
  107. }