• Javascript与C#编码解码


    (一) Javascript与C#编码解码的对应关系

    http://www.jb51.net/article/44062.htm

    这篇文章主要是对JS与C#编码解码进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助。

     
     
    escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z
    encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
    encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
     
     

    1.

    JS: (  escape()/unescape()  )

        js使用数据时可以使用escape。  0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。

    编码:escape()    解码:unescape()

    C#:(  System.Web.HttpUtility.UrlEncode()/System.Web.HttpUtility.UrlDecode()  )

    2.

    JS: (  encodeURI()/decodeURI()  )

        进行url跳转时可以整体使用encodeURI     例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21");

    解码使用decodeURI();

    C#: decodeURIComponent

    3.

    JS: (  encodeURIComponent()/decodeURIComponent()  )

    传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。                          

    例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7&

    u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a& gt;');</script>

    解码使用decodeURIComponent()

    C#:

    [HttpContext.Current.]Server.UrlDecode

    [HttpContext.Current.]Server.UrlEncode

    (二) js中escape对应的C#解码函数

    http://www.js123.net/t/n/n/2012/9/17/n_82.shtml

    js中escape对应的C#解码函数 System.Web.HttpUtility.UrlDecode(s) //注意编码

    需要注意的几点:

    1、HttpUtility.UrlEncode,HttpUtility.UrlDecode是静态方法,而Server.UrlEncode,Server.UrlDecode是实例方法。

    2、Server是HttpServerUtility类的实例,是System.Web.UI.Page的属性。

    3、用HttpUtility.UrlEncode编码后的字符串和用Server.UrlEncode进行编码后的字符串对象不一样:

    例如:

    string url="http://search.99read.com/index.aspx?book_search=all&main_str=奥迷尔";

    Response.Write(HttpUtility.UrlEncode(url)); Response.Write("<br>");

    Response.Write(Server.UrlEncode(url));

    输出结果是:

    http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%e5%a5%a5%e8%bf%b7%e5%b0%94

    http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%b0%c2%c3%d4%b6%fb

    原因:Server.UrlEncode的编码方式是按照本地程序设置的编码方式进行编码的,而HttpUtility.UrlEncode是默认的按照.net的utf-8格式进行编码的。

    如果改一下程序:

    string url1="http://search.99read.com/index.aspx?book_search=all&main_str=奥迷尔";

    Response.Write(HttpUtility.UrlEncode(url1,System.Text.Encoding.GetEncoding("GB2312")));

    Response.Write("<br>"); Response.Write(Server.UrlEncode(url1));

    输出的结果是:

    http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%b0%c2%c3%d4%b6%fb

    http%3a%2f%2fsearch.99read.com%2findex.aspx%3fbook_search%3dall%26main_str%3d%b0%c2%c3%d4%b6%fb

    3、有时候可能别的系统传递过来的url是用别的编码方式编码的。

    介绍自己编写的一个方法,可以获取指定编码格式的QueryString。

    public string GetNonNullQueryString(string key,Encoding encoding)
    {
      //引用System.Collections.Specialized和System.Text命名空间
      string stringValue;
      System.Collections.Specialized.NameValueCollection encodingQueryString;
      //该方法是在2.0中新增的
      encodingQueryString = HttpUtility.ParseQueryString(Request.Url.Query,encoding);
      //'里面的key就是你提交的参数的Key
      return encodingQueryString[key] != null ? encodingQueryString[key].Trim() : ""; 
    }

    调用:

    string url = GetNonNullQueryString("url",Encoding.UTF8).Trim();
    

    escape对应C#的编码方式 
    escape和HttpUtility.UrlEncodeUnicode()编码方式是一样的。

    调用: string url = GetNonNullQueryString("url",Encoding.UTF8).Trim();

    escape对应C#的编码方式  escape和HttpUtility.UrlEncodeUnicode()编码方式是一样的


    (三) 编码解码时应注意网页的页面编码方式

    http://blog.csdn.net/yenange/article/details/6417806

    encodeURIComponent() / decodeURIComponent()

    encodeURI()/decodeURI()

    注意上面两对javascript函数使用的编码多是utf-8,如果页面使用编码不是utf-8就需要做另外的处理。  

    asp.net 发数据给 javascript

    在页面使用gb2312时 encodeString=HttpUtility.UrlEncode("中问是中问http://www.gyzs.net/", System.Text.Encoding.UTF8)

    接收

    <script>document.write(decodeURIComponent('<%=encodeString %>'));</script>  

    javascritp 发数据给asp.net

    页面中有如下js脚本

    <script>document.write("<a href='?t=" +encodeURI('中问是中问http://www.gyzs.net') +"' >点我</a>");</script>

    如果web.config中配置使用gb2312那么就要做如下处理

    Response.Write(HttpUtility.UrlDecode( Server.UrlEncode( Request["t"]),System.Text.Encoding.UTF8));

    如果web.config中使用的utf-8那么直接使用Request["t"]就可以。

    另外要注意表单提交使用post方法时,会对表单里的数据进行 url编码,注意不要重复。

    (四) 从此不再惧怕URI编码:JavaScript及C# URI编码详解

    http://www.cnblogs.com/artwl/archive/2012/03/07/2382848.html

    混乱的URI编码

      JavaScript中编码有三种方法:escape、encodeURI、encodeURIComponent

      C#中编码主要方法:HttpUtility.UrlEncode、Server.UrlEncode、Uri.EscapeUriString、Uri.EscapeDataString

      JavaScript中的还好,只提供了三个,C#中主要用的就有这么多,还没有列出其他编码(HTML),一多就弄不明白,弄不明白就心生恐惧,心生恐惧就变得苦逼,本文就向大家详细解释在JavaScript及C#中如何对URI进行编码的方法(注:本文不涉及到其他编码)。

    escape:不推荐使用

      原因:eacape是BOM中的方法,只能对ASCII符号正确编码,而encodeURI、encodeURIComponent可以对所有的Unicode符号编码。ECMAScript v3 反对使用该方法,应用使用 decodeURI() 和 decodeURIComponent() 替代它。

      escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z

    encodeURI:用于对网址编码(不包含参数)

      encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

      encodeURI就是为这个而设计的。encodeURI不对URI中的特殊字符进行编码,如冒号(:)、斜杠(/)。下面看个示例:

    encodeURI("http://www.cnblogs.com/a file with spaces.html") // outputs http://www.cnblogs.com/a%20file%20with%20spaces.html

      可以看到仅仅把空格替换成了20%,所以此方法可用于对网址进行编码。

      由于encodeURI不对冒号(:)、斜杠(/)进行编码,所以如果参数(如把网址作为参数)中包含冒号(:)、斜杠(/),就会解析出错,所以此方法不能对参数进行编码。

    encodeURIComponent:用于对网址参数进行编码

      encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

      可以看到此方法对:/都进行了编码,所以不能用它来对网址进行编码。由于此方法对中文,空格,井号(#),斜线(/),冒号(:)都进行了编码,所以适合对URI中的参数进行编码。看下面的示例:

    var param="博客园"; var url="http://www.cnblogs.com/?key="+encodeURIComponent(param)+"&page=1"; console.log(url);//outputs http://www.cnblogs.com/?key=%E5%8D%9A%E5%AE%A2%E5%9B%AD&page=1

      可以看到,这正是我们想要的结果(这里只对需要编码的参数(page=1不需要编码)进行了编码)。

    Server.UrlEncode && HttpUtility.UrlEncode:不推荐

      把这两个放到一起说是因为这两个方法在绝大多数情况下是一样的。它们的区别是HttpUtility.UrlEncode默认使用UTF8格式编码,而Server.UrlEncode是使用系统预设格式编码,Server.UrlEncode使用系統预设编码做为参数调用HttpUtility.UrlEncode编码,所以如果系统全局都用UTF8格式编码,这两个方法就是一样的。

      这两个方法是怎么编码的呢,我们来看个示例:

    string url1 = "http://www.cnblogs.com/a file with spaces.html?a=1&b=博客园#abc"; Response.Write(HttpUtility.UrlEncode(url1) );
    //output http%3a%2f%2fwww.cnblogs.com%2fa+file+with+spaces.html%3fa%3d1%26b%3d%e5%8d%9a%e5%ae%a2%e5%9b%ad%23abc

      由上面的例子我们可以看出,HttpUtility.UrlEncode对冒号(:)和斜杠(/)进行了编码,所以不能用来对网址进行编码。

      那么能不能对参数进行编码呢,答案也是否定的。因为在参数中空格应该被编码为%20而不是被HttpUtility.UrlEncode编码为加号(+),所以不推荐用这两个方法对URI进行编码。

    Uri.EscapeUriString:用于对网址编码(不包含参数)

      我们还是用例子说话:

    string url1 = "http://www.cnblogs.com/a file with spaces.html?a=1&b=博客园#abc"; Response.Write( Uri.EscapeUriString(url1)); //outputs: http://www.cnblogs.com/a%20file%20with%20spaces.html?a=1&b=%E5%8D%9A%E5%AE%A2%E5%9B%AD#abc

      可以看出,Uri.EscapeUriString对空格进行了编码,也对中文进行了编码,但对冒号(:)、斜杠(/)和井号(#)未编码,所以此方法可以用于网址进行编码,但不能对参数进行编码,作用类似JavaScript中的encodeURI方法。

    Uri.EscapeDataString:用于对网址参数进行编码

      仍然用例子说话:

    string url1 = "http://www.cnblogs.com/a file with spaces.html?a=1&b=博客园#abc"; Response.Write(Uri.EscapeDataString(url1)); //outputs: http%3A%2F%2Fwww.cnblogs.com%2Fa%20file%20with%20spaces.html%3Fa%3D1%26b%3D%E5%8D%9A%E5%AE%A2%E5%9B%AD%23abc

      可以看出,Uri.EscapeDataString对冒号(:)、斜杠(/)、空格、中文、井号(#)都进行了编码,所以此方法不可以用于网址进行编码,但可以用于对参数进行编码,作用类似JavaScript中的encodeURIComponent方法。

    小结

      在JavaScript中推荐的做法是用encodeURI对URI的网址部分编码,用encodeURIComponent对URI中传递的参数进行编码。

      在C#中推荐的做法是用Uri.EscapeUriString对URI的网址部分编码,用Uri.EscapeDataString对URI中传递的参数进行编码。

      解码部分就不说了,与编码方法相对应。

  • 相关阅读:
    windows系统桌面美化
    Jenkins 显示语言-英文切换中文
    Jenkins 任务定时
    Jenkins 结合 ANT 发送测试报告
    ANT 的使用
    虚拟机与主机之间文件传输
    【译】使用WebDriver采样器将JMeter与Selenium集成
    Newman基本使用
    autoIt中文手册
    selenium控制超链接在当前标签页中打开或重新打开一个标签页
  • 原文地址:https://www.cnblogs.com/netserver/p/4523520.html
Copyright © 2020-2023  润新知