代码如下:
string url = "http://www.abc.com/这里是中文测试参数"; // 方式一 // 编码 http%3a%2f%2fwww.abc.com%2f%e8%bf%99%e9%87%8c%e6%98%af%e4%b8%ad%e6%96%87%e6%b5%8b%e8%af%95%e5%8f%82%e6%95%b0 string urlEncode = System.Web.HttpUtility.UrlEncode(url); // 解码 string urlDecode = System.Web.HttpUtility.UrlDecode(urlEncode); // 编码时指定编码 System.Web.HttpUtility.UrlEncode(url, System.Text.Encoding.UTF8); System.Web.HttpUtility.UrlEncode(url, System.Text.Encoding.Unicode); System.Web.HttpUtility.UrlEncode(url, System.Text.Encoding.GetEncoding("GB2312")); // 解码时指定编码 System.Web.HttpUtility.UrlDecode(url, System.Text.Encoding.UTF8); System.Web.HttpUtility.UrlDecode(url, System.Text.Encoding.Unicode); System.Web.HttpUtility.UrlDecode(url, System.Text.Encoding.GetEncoding("GB2312")); // 方式二, 只能在aspx, ashx, controller中使用 // 编码 http%3a%2f%2fwww.abc.com%2f%e8%bf%99%e9%87%8c%e6%98%af%e4%b8%ad%e6%96%87%e6%b5%8b%e8%af%95%e5%8f%82%e6%95%b0 string urlEncode2 = System.Web.HttpContext.Current.Server.UrlEncode(url); // 解码 string urlDecode2 = System.Web.HttpUtility.UrlDecode(urlEncode);