• c# 对Url 解码编码


       /// <summary>
            /// 对Url进行编码
            /// </summary>
            /// <param name="url">url</param>
            /// <param name="isUpper">编码字符是否转成大写,范例,"http://"转成"http%3A%2F%2F"</param>
            public static string UrlEncode(string url, bool isUpper = false)
            {
                return UrlEncode(url, Encoding.UTF8, isUpper);
            }
    
            /// <summary>
            /// 对Url进行编码
            /// </summary>
            /// <param name="url">url</param>
            /// <param name="encoding">字符编码</param>
            /// <param name="isUpper">编码字符是否转成大写,范例,"http://"转成"http%3A%2F%2F"</param>
            public static string UrlEncode(string url, Encoding encoding, bool isUpper = false)
            {
                var result = HttpUtility.UrlEncode(url, encoding);
                if (!isUpper)
                    return result;
                return GetUpperEncode(result);
            }
    
            /// <summary>
            /// 获取大写编码字符串
            /// </summary>
            private static string GetUpperEncode(string encode)
            {
                var result = new StringBuilder();
                int index = int.MinValue;
                for (int i = 0; i < encode.Length; 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();
            }
      /// <summary>
            /// 对Url进行解码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码
            /// </summary>
            /// <param name="url">url</param>
            public static string UrlDecode(string url)
            {
                return HttpUtility.UrlDecode(url);
            }
    
            /// <summary>
            /// 对Url进行解码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码
            /// </summary>
            /// <param name="url">url</param>
            /// <param name="encoding">字符编码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码</param>
            public static string UrlDecode(string url, Encoding encoding)
            {
                return HttpUtility.UrlDecode(url, encoding);
            }
    从此山高路远,纵马扬鞭。愿往后旅途,三冬暖,春不寒,天黑有灯,下雨有伞。此生尽兴,不负勇往。
  • 相关阅读:
    ubuntu9.10在AMD 双核上的安装
    修改wamp默认安装下MySQL的root账户密码
    IE6 IE7 IE8 火狐浏览器兼容问题
    在windows xp下安装ubuntu9.10
    制作Ubuntu U 盘启动盘在ubuntu12.04中
    使用StackTraceElement封装,打印调试代码!
    String字符串拼接原理剖析
    List中的数据如何根据对象的某一个或多个字段排序引出Comparable和comparator的使用
    String类的split方法
    通过例子看六种异常处理的陋习
  • 原文地址:https://www.cnblogs.com/feizianquan/p/9734409.html
Copyright © 2020-2023  润新知