c# 对Url 解码编码
程序员文章站
2022-04-25 18:23:50
/// /// 对Url进行编码 /// /// url /// 编码字符是否转成大写,范例,"http://"转成"http%3A%2F%2F" public static string UrlEncode(string url, bool isUpper = false) { ... ......
/// <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); }