WebRequest提交时中文乱码问题的解决

    技术2022-05-11  64

    在使用WebRequest进行提交中文时,对方的URL收到的会是乱码,解决方法很简单,对提交的中文进行编码即可.

    HttpUtility.UrlEncode("中文内容", Encoding.GetEncoding("GB2312"))

    然后在Web.config里设置<system.web> ...... <globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" fileEncoding="gb2312" /> ...... </system.web> 或者:aspx文件中:<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

    基本上可以解决此问题了.

    另外,在redirect时,有时会碰到中文字符丢失的问题,可通过以下方法解决:

    string paraVal= Server.UrlEncode("someValue");//URL加密Response.Redirect("workerxinxi.aspx?bhao=" + paraVal);//其他如带汉字的参数同样workerxinxi.aspx页面:string paraVal=Server.UrlDecode(Request["bhao"].Tostring());/

    Js:

    <script language="JavaScript"> function GoUrl() { var Name = "中文参数"; location.href = "B.aspx?Name="+escape(Name); } </script> <body οnclick="GoUrl()"> >> 进行接收 string Name = Request.QueryString["Name"]; Response.Write(Server.UrlDecode(Name));


    最新回复(0)