123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Globalization;
- namespace COSXML.Utils
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public sealed class URLEncodeUtils
- {
-
- public const string URLAllowChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
-
-
-
-
-
- public static string EncodePathOfURL(string path)
- {
- if (String.IsNullOrEmpty(path))
- {
- return String.Empty;
- }
- char separator = '/';
- int start = 0;
- int length = path.Length;
- int index = path.IndexOf(separator, start);
- StringBuilder result = new StringBuilder();
- while (index != -1 && start < length)
- {
- if (start != index)
- {
- result.Append(Encode(path.Substring(start, index - start)));
- }
- result.Append(separator);
- start = index + 1;
- index = path.IndexOf(separator, start);
- }
- if (start < length)
- {
- result.Append(Encode(path.Substring(start)));
- }
- return result.ToString();
- }
- public static string Encode(string value)
- {
- return Encode(value, Encoding.UTF8);
- }
-
-
-
-
-
-
-
- public static string Encode(string value, Encoding encoding)
- {
- if (String.IsNullOrEmpty(value))
- {
- return String.Empty;
- }
-
-
- StringBuilder result = new StringBuilder(value.Length * 2);
- byte[] strToBytes = encoding.GetBytes(value);
- foreach (byte b in strToBytes)
- {
- char ch = (char)b;
- if (URLAllowChars.IndexOf(ch) != -1)
- {
- result.Append(ch);
- }
- else
- {
- result.Append('%').Append(String.Format(CultureInfo.InvariantCulture, "{0:X2}", (int)b));
- }
- }
- return result.ToString();
- }
-
-
-
-
-
- public static string Decode(string valueEncode)
- {
- if (String.IsNullOrEmpty(valueEncode))
- {
- return String.Empty;
- }
- valueEncode.Replace('+', ' ');
- return Uri.UnescapeDataString(valueEncode);
- }
- }
- }
|