URL编码

    /// <summary>
    /// URL编码
    /// </summary>
    /// <param name="value">The value to Url encode</param>
    /// <returns>Returns a Url encoded string</returns>
    public string UrlEncode(string value)
    {
        StringBuilder result = new StringBuilder();

        string unreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
        foreach (char symbol in value)
        {
            if (unreservedChars.IndexOf(symbol) != -1)
            {
                result.Append(symbol);
            }
            else
            {
                result.Append('%' + String.Format("{0:X2}", (int)symbol));
            }
        }

        return result.ToString();
    }

 

posted @ 2021-02-02 17:55  春夏秋冬的千山万水  阅读(54)  评论(0)    收藏  举报