示例#1
0
        /// <summary>
        /// URL-encode according to RFC 3986
        /// (Thanks to http://www.netmftoolbox.com/ for guidance)
        /// </summary>
        /// <param name="input">The URL to be encoded.</param>
        /// <returns>Returns a string in which all non-alphanumeric characters except -_.~ have been replaced with a percent (%) sign followed by two hex digits.</returns>
        public static string UrlEncode(string input)
        {
            string result = "";

            for (int count = 0; count < input.Length; ++count)
            {
                byte charCode = (byte)(input.ToCharArray()[count]);
                if (
                    charCode == 0x2d ||                    // -
                    charCode == 0x5f ||                    // _
                    charCode == 0x2e ||                    // .
                    charCode == 0x7e ||                    // ~
                    (charCode > 0x2f && charCode < 0x3a) || // 0-9
                    (charCode > 0x40 && charCode < 0x5b) || // A-Z
                    (charCode > 0x60 && charCode < 0x7b)   // a-z
                    )
                {
                    result += input.Substring(count, 1);
                }
                else
                {
                    // represent the hex value
                    result += "%" + AbstractNumberUtils.UInt32ToHex(charCode, 2);
                }
            }

            return(result);
        }
示例#2
0
        /// <summary>
        /// URL-decode according to RFC 3986
        /// (Thanks to http://www.netmftoolbox.com/ for guidance)
        /// </summary>
        /// <param name="input">The URL to be decoded.</param>
        /// <returns>Returns a string in which original characters</returns>
        public static string UrlDecode(string input)
        {
            string result = "";

            for (int count = 0; count < input.Length; ++count)
            {
                string charCode = input.Substring(count, 1);
                if (charCode == "%")
                {
                    // Encoded character
                    string hexVal = input.Substring(++count, 2);
                    ++count;
                    result += (char)AbstractNumberUtils.Hex2UInt32(hexVal);
                }
                else
                {
                    // Normal character
                    result += charCode;
                }
            }

            return(result);
        }