/// <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); }
原文地址:https://www.cnblogs.com/yuanzijian-ruiec/p/9734409.html
时间: 2024-10-06 23:16:36