jquery 编码解码
中文转Unicode:HttpUtility.UrlEncodeUnicode(string str); 转换后中文格式:"%uxxxx" 举例:"柳_abc123" 转换结果是:"%u67f3_abc123" Unicode转中文1:HttpUtility.UrlDecode(string str); str格式:"%uxxxx" ,举例:"%u67f3_abc123" Unicode转中文2:Regex.Unescape(string str); str格式:"\uxxxx" ,举例:"\u67f3_abc123" 参考资料:http://hi.baidu.com/eegcn/blog/item/7315f799a2e57f136f068c55.html 1.window.escape()与HttpUtility.UrlEncodeUnicode()编码格式一样:将一个汉字编码为%uxxxx格式 不会被window.escape编码的字符有:@ _ - . * / + 这与http://www.w3school.com.cn/js/jsref_escape.asp上的解释不符合 2.window.encodeURIComponent()与HttpUtility.UrlEncode()编码格式一样:将一个汉字编码为%xx%xx%xx的格式 不会被window.encodeURIComponent编码的字符有:'()*-._!~ 这与http://www.w3school.com.cn/js/jsref_encodeURIComponent.asp解释相符合 不会被HttpUtility.UrlEncode编码的字符有:'()*-._!相比较而言,HttpUtility.UrlEncode比window.encodeURIComponent多一个 ~ 编码 3.不会被window.encodeURI编码的字符有:-_.!*();/?:@&=$,# 与encodeURIComponent对比,发现encodeURI不对:;/?:@&=+$,#这些用于分隔 URI 组件的标点符号进行编码 Asp.Net编码与JS编码的区别: 1. 不会被HttpUtility.UrlEncodeUnicode编码的字符与不会被HttpUtility.UrlEncode编码的字符一样,而escape和encodeURIComponent不编码的字符不一样 2. HttpUtility.UrlEncode和HttpUtility.UrlEncodeUnicode会对/编码,而escape和encodeURIComponent会对/编码,encodeURI不会对/编码 3. HttpUtility.UrlEncode()和HttpUtility.UrlEncodeUnicode()会把空格编码为 +,而escape,encodeURIComponent,encodeURI都会将空格编码为%20 使用ajax提交一个字符串: 1. xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); var postStr="val={name:'梅小伟',age:19}"; xmlHttp.send(postStr); 客户端发送请求如下: POST /index.aspx HTTP/1.1 Accept: */* Accept-Language: zh-cn Referer: http://localhost.:3910/Default.aspx Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; baiduie8) Host: localhost.:3910 Content-Length: 29 Connection: Keep-Alive Pragma: no-cache val={name:'梅小伟',age:19}//发现这里没有经过编码,直接以2进制方式发送 在服务端index.aspx中打断点,发现Request.Form为:val=%7bname%3a'%u6885%u5c0f%u4f1f'%2cage%3a19%7d(这里使用了escape编码)使用Request.Form[0]取出的值和使用Request.Form["val"]取出的都为“{name:'梅小伟',age:19}” 2. xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); var postStr=window.encodeURIComponent("val={name:'梅小伟',age:19}"); xmlHttp.send(postStr); 客户端发送请求如下: POST /index.aspx HTTP/1.1 Accept: */* Accept-Language: zh-cn Referer: http://localhost.:3910/Default.aspx Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; CIBA; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; baiduie8) Host: localhost.:3910 Content-Length: 59 Connection: Keep-Alive Pragma: no-cache val%3D%7Bname%3A'%E6%A2%85%E5%B0%8F%E4%BC%9F'%2Cage%3A19%7D//发现这里使用了window.encodeURIComponent加码 在服务端index.aspx中打断点,发现Request.Form为:val%3d%7bname%3a'%u6885%u5c0f%u4f1f'%2cage%3a19%7d(这里居然使用了escape编码,而不是encodeURIComponent编码),使用Request.Form[0]取出的值为“val={name:'梅小伟',age:19}”,使用Request.Form["val"]取出的值为null(这是因为客户端发送请求时将=编码为%3d了,如果使用window.encodeURI这里就能取出Request.Form["val"]为:“{name:'梅小伟',age:19}”了) 总结:不是使用get或者post,只要都是使用form的enctype属性的默认值application/x-www-form-urlencoded,所以如果你要传的值都会经过window.encodeURIComponent()编码再传送(除了值包含空格不会被编码为%20,而是编码为+).传到服务器后,可以用Server.UrlDecode()进行解码。但是要注意,不管是get方式还是post方式,enctype为application/x-www-form-urlencoded还是multipart/form-data,用asp.net在后台查看Request.QueryString和Request.Form的时候,中文又变成了escape编码格式,例如Request.Form=__VIEWSTATE=%2fwEPDwUJNzgzNDMwNTMzZGSvF5y%2bl0lztppRS7QNr4qmrF4KTw%3d%3d&mm=%u6556%u5fb7%u8428%u7684(英语字母不会被编码,而一些符号使用encodeURIComponent和escape编码后相同,如=,$等等)。 为什么优先使用encodeURIComponent而不是escape? escape方法并不编码字符+。而我们知道,在用户提交的表单字段中,如果有空格,则会被转化为+字符,而服务器解析的时候则会认为+号代表空格。由于这个缺陷,escape方法并不能正确地处理所有的非ASCII字符,你应当尽量避免使用escape方法,取而代之,你最好选择 encodeURIComponent()方法。 |
- 上一篇 »python3中编码和解码
- 下一篇 »JAVA-前台编码,后台解码