示例#1
0
        /// <summary>
        /// Does url encoding using uppercase since that is needed for .Net
        /// </summary>
        /// <param name="Input">Input string</param>
        /// <returns>Url encoded string</returns>
        protected string UrlEncode(string Input)
        {
            StringBuilder Result = new StringBuilder();

            for (int x = 0; x < Input.Length; ++x)
            {
                if (UnreservedChars.IndexOf(Input[x]) != -1)
                {
                    Result.Append(Input[x]);
                }
                else
                {
                    Result.Append("%").Append(String.Format("{0:X2}", (int)Input[x]));
                }
            }
            return(Result.ToString());
        }
示例#2
0
        ///<summary>
        ///2バイト文字も考慮したUrlエンコード
        ///</summary>
        ///<param name="stringToEncode">エンコードする文字列</param>
        ///<returns>エンコード結果文字列</returns>
        protected string UrlEncode(string stringToEncode)
        {
            const string  UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
            StringBuilder sb = new StringBuilder();

            byte[] bytes = Encoding.UTF8.GetBytes(stringToEncode);

            foreach (byte b in bytes)
            {
                if (UnreservedChars.IndexOf((char)b) != -1)
                {
                    sb.Append((char)b);
                }
                else
                {
                    sb.AppendFormat("%{0:X2}", b);
                }
            }
            return(sb.ToString());
        }
        // can't use HttpUtility.UrlEncode(str) since it encodes space to + (expected %20).
        // can't use HttpUtility.UrlPathEncode(str) since it does not encodes / (expected %2F).
        private static string UrlEncode(string str)
        {
            const string UnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";

            StringBuilder result = new StringBuilder();

            byte[] data = Encoding.UTF8.GetBytes(str);
            int    len  = data.Length;

            for (int i = 0; i < len; i++)
            {
                int c = data[i];
                if (c < 0x80 && UnreservedChars.IndexOf((char)c) != -1)
                {
                    result.Append((char)c);
                }
                else
                {
                    result.Append('%' + string.Format("{0:X2}", (int)data[i]));
                }
            }

            return(result.ToString());
        }