zoukankan      html  css  js  c++  java
  • asp.net 页面通过URL传参中文乱码的三种解决方案

    1.编码

    string state=Server.UrlEncode(stateName.Text.Trim());
    Response.Redirect("aaa.aspx?state="+state+"");
     
    2.解码(aaa.aspx)

    string state= Server.UrlDecode(Request.QueryString["state"].ToString());

    是GB2312编码的,参数传值是UTF-8编码的

    解决的方法一般有3种:

    1.设置web.config文件

    <system.web> 
    ...... 
    <globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" fileEncoding="gb2312" /> 
    ...... 
    </system.web>

    2.传递中文之前,将要传递的中文参数进行编码,在接收时再进行解码。 
    >> 进行传递

    string Name = "中文参数"; 
    Response.Redirect("B.aspx?Name="+Server.UrlEncode(Name)) ;

    >> 进行接收

    string Name = Request.QueryString["Name"]; 
    Response.Write(Server.UrlDecode(Name)) ;

    3.如果是从 .HTML 文件向 .Aspx 文件进行传递中文参数的话(即不从后台用 Redirect()方法进行 Url 转换)。一样要将传递的中文参数进行编码,在接收时再进行解码。 
    >> 进行传递

    <script language="JavaScript"> 
    function GoUrl() 

    var Name = "中文参数"; 
    location.href = "B.aspx?Name="+escape(Name) ; 

    <body onclick="GoUrl()">


    >> 进行接收

    string Name = Request.QueryString["Name"]; 
    Response.Write(Server.UrlDecode(Name)) ; 
    总结:
    一般来说。设置web.config文件就可以了。但是如果你用 JavaScript 调用 webservice 方法的话(往webservice里面传递中文参数)。设置 web.config 文件好象无效。 
    或用

    Response.Redirect("test1.aspx?111="+System.Web.HttpUtility.UrlEncode("中医药")) ; 
    //建议使用最后如果是从其他的页面获取中文参数没有乱码,那就更简单了

    string message ="http://localhost/Test/test1.aspx?111="+System.Web.HttpUtility.UrlEncode("中医药");

    http:
    //你要获取某个页面的返回值的地址"
    //发送请求
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(message) ;
    //接受请求
    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse() ;
    Stream receiveStream = myHttpWebResponse.GetResponseStream() ;
    StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.GetEncoding("GB2312")) ;
    //此为要取页面的返回值输出的返回结果
    returnValue = readStream.ReadToEnd();

  • 相关阅读:
    2.列出目录内容
    mongo2csv
    Python-uiautomator使用说明文档
    uiautomator设备和选择器~Python详解
    hadoop1.2开发环境搭建
    postgresql 查询某一个表中的所有字段
    VolgaCTF 2020 Qualifier Library
    VolgaCTF 2020 Qualifier Newsletter
    2019-2020-2 网络对抗技术 20175211 Exp3 免杀原理与实践
    BJDCTF 2nd Writeup
  • 原文地址:https://www.cnblogs.com/freedomlan/p/4462372.html
Copyright © 2011-2022 走看看