URI编码

    技术2022-05-20  37

    http://www.sdvil.com/编码/89

    url 编码

    Leave a comment (0)

    通过url可以访问到网络上的内容,通常它的格式是这样的, protocol://domain/path/filename 为了可以在url 上传输数据, 可以在后面加了 ?key=value&key=value , 可以看到 : / ? & 符号是有特殊意义的, 任意的加入这些字符,就有可能改变url 的意义, 这样就需要对这些字符和那些非ascii 字符集的字符进行编码, 以保证url 的正确的传输。

    编码有两种方式, 一个是比较老的 %u + unicode 的方式,如中国的unicode编码是4E2D, 56FD , %u4E2D%u56FD 还有一种是 用% 分割编码的方式,比如中国, utf-8 编码为 e4b8 ade5 9bbd 其对应的url 编码为 中国

    这两种方式也是js 里两组函数对字符进行url 编码的结果, escape 和unescape 是第一种方式,encodeURI和decodeURI 是第二种方式。 encodeURIComponent和decodeURIComponent 也是js 里常用的url 编码的函数, 跟encodeURI和decodeURI 这组函数的区别是它们编码的字符更多一点, 看名字就知道,一个是针对组件的, 编码完后可做为 请求数据key value 中的value 用, 还有一个是针对url 来说, 对/ :@ 等不编码, 想想, 要是对这些也编码了, 那这个url 就不能用于请求网络数据了。 下面列出几个函数的安全字符(不编码的字符)

    安全字符escape(69个)*/@+-._0-9a-zA-ZencodeURI(82个)!#$&’()*+,/:;=?@-._~0-9a-zA-ZencodeURIComponent(71个)!’()*-._~0-9a-zA-Z

    也列一些常用的字符编码以备俺查用

    !*“‘();:@&!*"'();:@&=+$,/?%#[]=+$,/?%#[]

    说完这些, 读者对url 编码有一定的了解了吧。

    看完这些基础的, 看看浏览器是怎么处理这些数据的。

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <? php header("Content-type: text/html; charset = gb2312 "); //header("Content-type: text/html; charset = utf -8"); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"" "http://www.w3.org/TR/html4/loose.dtd "> < html > < head > < meta http-equiv = "content-type" content = "text/html; charset=utf-8" /> <!--<meta http-equiv="content-type" content="text/html; charset=gb2312" />--> < title >Test_encoding</ title > </ head > < body > < form action = "test_encoding.html" method = "get" enctype = "application/x-www-form-urlencoded" > < input type = "text" name = "text" value = "中国" > < input type = "file" name = "file" value = "" > < p >< input type = "submit" value = "Continue → GET url" ></ p > </ form > < form action = "test_encoding.html" method = "POST" enctype = "application/x-www-form-urlencoded" > < input type = "text" name = "text" value = "中国" > < input type = "file" name = "file" value = "" > < p >< input type = "submit" value = "Continue → GET url" ></ p > </ form > < form action = "test_encoding.html" method = "GET" enctype = "multipart/form-data" > < input type = "text" name = "text" value = "中国" > < input type = "file" name = "file" value = "" > < p >< input type = "submit" value = "Continue → GET data" ></ p > </ form > < form action = "test_encoding.html" method = "POST" enctype = "multipart/form-data" > < input type = "text" name = "text" value = "中国" > < input type = "file" name = "file" value = "" > < p >< input type = "submit" value = "Continue → GET data" ></ p > </ form > < a href = "test_encoding.html?t=中国" >中国</ a > </ body > </ html >

    这个例子有几个点: 1. 页面编码为 utf-8 , 用get 请求数据 ,抓包可以看到 test_encoding.html?text=中国&file=D:\中国.docx

    把页面代码强制改为 gb2312 , 把页面上的文字删除, 重新输入 中国, 抓包可以看到 test_encoding.html?text=

    转载请注明原文地址: https://ibbs.8miu.com/read-2224378.html

    最新回复(0)