對網址進行UrlEncode編碼
//對網址進行UrlEncode編碼,接收后再進行URLDecode解碼 public static string UrlEncode(string str) { string tmpStr = ""; if (str != "" && str != null) { StringBuilder sb = new StringBuilder(); byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); for (int i = 0; i < byStr.Length; i++) { sb.Append(@"%" + Convert.ToString(byStr[i], 16)); } tmpStr = sb.ToString(); } return (tmpStr); }
//對網址進行UrlEncode編碼,接收后再進行URLDecode解碼
public static string UrlEncode(string str)
{
string tmpStr = "";
if (str != "" && str != null)
StringBuilder sb = new StringBuilder();
byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str);
for (int i = 0; i < byStr.Length; i++)
sb.Append(@"%" + Convert.ToString(byStr[i], 16));
}
tmpStr = sb.ToString();
return (tmpStr);
對網址進行UrlDecode編碼
/// <summary> /// 對字符進行UrlDecode解碼 /// Encoding轉string格式 /// </summary> /// <param name="encodString"></param> /// <param name="encod">編碼格式</param> /// <returns></returns> public static string UrlDecode(string encodString) { string text = System.Web.HttpUtility.UrlDecode(encodString, System.Text.Encoding.UTF8); return text; }
/// <summary>
/// 對字符進行UrlDecode解碼
/// Encoding轉string格式
/// </summary>
/// <param name="encodString"></param>
/// <param name="encod">編碼格式</param>
/// <returns></returns>
public static string UrlDecode(string encodString)
string text = System.Web.HttpUtility.UrlDecode(encodString, System.Text.Encoding.UTF8);
return text;