在WAP中经常要遇到网址编码转换的问题,在ASP解决起来还是比较麻烦,但是在C#中做起来就容易多了,今天整理了一下,用ASP.NET做了一个例子,代码如下: 在WAP中经常要遇到网址编码转换的问题,在ASP解决起来还是比较麻烦,但是在C#中做起来就容易多了,今天整理了一下,用ASP.NET做了一个例子,代码如下: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
namespace study { /// <summary> /// UrlEncode_ 的摘要说明。 /// </summary> public class UrlEncode_ : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { string strName="刘德华"; string strEncodeNameGB2312=System.Web.HttpUtility.UrlEncode(strName,System.Text.Encoding.GetEncoding("GB2312")).ToUpper(); string strDecodeGB2312=System.Web.HttpUtility.UrlDecode(strEncodeNameGB2312,System.Text.Encoding.GetEncoding("GB2312")); //如果设定为默认编码则 System.Text.Encoding.Default
Response.Write( "网址编码过的文字(GB2312):"+ strEncodeNameGB2312 +"<br>" ); Response.Write( "经过解码的文字为(GB2312):"+ strDecodeGB2312 +"<p></p>" );
string strEncodeNameUTF8=System.Web.HttpUtility.UrlEncode(strName,System.Text.Encoding.GetEncoding("utf-8")).ToUpper(); string strDecodeUTF8=System.Web.HttpUtility.UrlDecode(strEncodeNameUTF8,System.Text.Encoding.GetEncoding("utf-8"));
Response.Write( "网址编码过的文字(UTF8):"+ strEncodeNameUTF8 +"<br>" ); Response.Write( "经过解码的文字为(UTF8):"+ strDecodeUTF8 +"<br>" ); }
#region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); }
/// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load);
} #endregion } } 备注 GetEncoding 方法依赖于基础平台支持大部分代码页。但是,对于下列情况提供系统支持:默认编码,即在执行此方法的计算机的区域设置中指定的编码;Little-Endian Unicode (UTF-16LE);Big-Endian Unicode (UTF-16BE);Windows 操作系统 (windows-1252);UTF-7;UTF-8;ASCII 以及 GB18030(简体中文)。 详见MSDN:ms-help://MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemtextencodingclassgetencodingtopic2.htm 本文出自 51CTO.COM技术博客 |