c# 对Url 解码编码

2021-07-08 19:06

阅读:395

标签:build   dex   rip   http   ring   函数   col   编码   private   

   /// 
        /// 对Url进行编码
        /// 
        /// url
        /// 编码字符是否转成大写,范例,"http://"转成"http%3A%2F%2F"
        public static string UrlEncode(string url, bool isUpper = false)
        {
            return UrlEncode(url, Encoding.UTF8, isUpper);
        }

        /// 
        /// 对Url进行编码
        /// 
        /// url
        /// 字符编码
        /// 编码字符是否转成大写,范例,"http://"转成"http%3A%2F%2F"
        public static string UrlEncode(string url, Encoding encoding, bool isUpper = false)
        {
            var result = HttpUtility.UrlEncode(url, encoding);
            if (!isUpper)
                return result;
            return GetUpperEncode(result);
        }

        /// 
        /// 获取大写编码字符串
        /// 
        private static string GetUpperEncode(string encode)
        {
            var result = new StringBuilder();
            int index = int.MinValue;
            for (int i = 0; i )
            {
                string character = encode[i].ToString();
                if (character == "%")
                    index = i;
                if (i - index == 1 || i - index == 2)
                    character = character.ToUpper();
                result.Append(character);
            }
            return result.ToString();
        }
  /// 
        /// 对Url进行解码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码
        /// 
        /// url
        public static string UrlDecode(string url)
        {
            return HttpUtility.UrlDecode(url);
        }

        /// 
        /// 对Url进行解码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码
        /// 
        /// url
        /// 字符编码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码
        public static string UrlDecode(string url, Encoding encoding)
        {
            return HttpUtility.UrlDecode(url, encoding);
        }

 

c# 对Url 解码编码

标签:build   dex   rip   http   ring   函数   col   编码   private   

原文地址:https://www.cnblogs.com/yuanzijian-ruiec/p/9734409.html


评论


亲,登录后才可以留言!