www.91084.com

GVKun编程网logo

urlencode值数组(url数组参数)

15

最近很多小伙伴都在问urlencode值数组和url数组参数这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展java中URLEncoder.encode与URLDecoder.de

最近很多小伙伴都在问urlencode值数组url数组参数这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展java中URLEncoder.encode与URLDecoder.decode处理url特殊参数的方法、JS escape urlencodeComponent urlencode 区别、PHP rawurlencode与urlencode函数的深入分析、php urlencode vs java URLEncoder.encode等相关知识,下面开始了哦!

本文目录一览:

urlencode值数组(url数组参数)

urlencode值数组(url数组参数)

我正在尝试使用urllib.urlencode在python中对字典进行urlencode。问题是,我必须编码一个数组。

结果需要为:

criterias%5B%5D=member&criterias%5B%5D=issue#unquoted: criterias[]=member&criterias[]=issue

但是我得到的结果是:

criterias=%5B%27member%27%2C+%27issue%27%5D#unquoted: criterias=[''member'',+''issue'']

我已经尝试了几种方法,但是似乎无法获得正确的结果。

import urllibcriterias = [''member'', ''issue'']params = {    ''criterias[]'': criterias,}print urllib.urlencode(params)

如果使用cgi.parse_qs解码正确的查询字符串,则会得到以下结果:

{''criterias[]'': [''member'', ''issue'']}

但是,如果我对该结果进行编码,则会返回错误的结果。有没有办法产生预期的结果?

答案1

小编典典

该解决方案比上面列出的解决方案简单得多。

>>> import urllib>>> params = {''criterias[]'': [''member'', ''issue'']}>>> >>> print urllib.urlencode(params, True)criterias%5B%5D=member&criterias%5B%5D=issue

注意真实。请参阅http://docs.python.org/library/urllib.html#urllib.urlencode剂量q变量。

附带说明,您不需要[]即可将其用作数组(这就是urllib不包含它的原因)。这意味着您不需要将[]添加到所有数组键中。

java中URLEncoder.encode与URLDecoder.decode处理url特殊参数的方法

java中URLEncoder.encode与URLDecoder.decode处理url特殊参数的方法

这篇文章主要给大家介绍了关于java中URLEncoder.encode与URLDecoder.decode处理url特殊参数的方法,文中介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。

最近在使用 url 的 queryString 传递参数时,因为参数的值,被DES加密了,而加密得到的是 Base64的编码字符串

类似于:

za4T8MHB/6mhmYgXB7IntyyOUL7CL++0jv5rFxaifVji8GDrcf+k8g==

显然 这里面含有了 特殊字符: / + = 等等,如果直接通过url 来传递该参数:

url = "xxxxx?param=" + "za4T8MHB/6mhmYgXB7IntyyOUL7CL++0jv5rFxaifVji8GDrcf+k8g==";

那么在服务端获得 param 会变成类似于下面的值:

"za4T8MHB/6mhmYgXB7IntyyOUL7Cl 0jv5rFxaifVji8GDrcf k8g=="

我们看到 三个 + 号消失了。

其原因就是:如果url参数值含有特殊字符时,需要使用 url 编码。

url = "xxxxx?param=" + URLEncoder.encode("xxx", "utf-8");

然后服务端获取时:

String param = URLDecoder.decode(param, "utf-8");

这样才能获得正确的值: "za4T8MHB/6mhmYgXB7IntyyOUL7CL++0jv5rFxaifVji8GDrcf+k8g=="

其实 js 中也有类似功能的函数:

参见:js中编码函数:escape,encodeURI,encodeURIComponent

注意事项:

URLEncoder should be the way to go. You only need to keep in mind to encode only the individual query string parameter name and/or value, not the entire URL, for sure not the query string parameter separator character & nor the parameter name-value separator character =

String q = "random word 拢500 bank $"; String url = "http://example.com/query?q=" + URLEncoder.encode(q, "UTF-8");

URLEncoder 必须 仅仅 编码 参数 或者参数的值,不能编码整个 url,也不能一起对 param=value 进行编码。而是应该: param=URLEncode(value, "utf-8")

或者 URLEncode(param, "utf-8")=URLEncode(value, "utf-8")

因为 url 中的 & 和 = 他们是作为参数之间 以及 参数和值之间的分隔符的。如果一起编码了,就无法区分他们了。

进一步参考文档:

https://www.talisman.org/~erlkonig/misc/lunatech%5Ewhat-every-webdev-must-kNow-about-url-encoding/

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对小编的支持。

JS escape urlencodeComponent urlencode 区别

JS escape urlencodeComponent urlencode 区别

js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent 1、 传递参数时需要使用encodeURIComponent,这样组合的url才不会被#等特殊字符截断。 例如: 2、 进行url跳转时可以整体使用encodeURI 例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21"); 3、 js使用数据时可以使用escape 例如:搜藏中history纪录。 4、 escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。 最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同) escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,'',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z encodeURIComponent不编码字符有71个:!, '',(,),*,-,.,_,~,0-9,a-z,A-Z 回复:javascript 中 escape,encodeURI 区别? escape() 方法: 所有空格、标点符号、重音字符,以及任何其他非ASCII字符都编码改为"%XX"的形式,xx是16进制数字所代表. escape和unescape的编码和解码功能, escape返回16进制编码的一种ISO拉丁字符集. unescape的功能将具有特殊值的16进制编码转换为ASCII字符串 举例: escape(''!@#$%^&*(){}[]=:/;?+\''"''): 结果:%21@%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%3B%3F+%27%22 encodeURI() 方法 Encodeuri方法返回一个编码后的URI. 因此,如果你将其结果用Decodeuri方法,原始的串会返回. Encodeuri的方法并不对以下字符编码:":"、"/"、"; "、"? ". 但可以使用 encodeuricomponent 的方法对这些字符进行encode. encodes,一种 Uniform Resource Identifier (URI)(URI)逐一替换某些字符,描述为UTF-8编码的特点. 例如 : encodeURI(''!@#$%^&*(){}[]=:/;?+\''"''): 结果:!@#$%25%5E&*()%7B%7D%5B%5D=:/;?+''%22 encodeURIComponent() 方法: encodeuricomponent 方法返回一个编码的URI. 因此,如果你将decodeuricomponent,原来的串会返回. 由于所有文字encodesencodeuricomponent方法都会进行编码,所以要小心,如果存在路径等串例如: "/FOLDER1/FOLDER2/DEFAULT.HTML". 加密后并不会,如果作为一个网络服务器的请求将会失效. 使用这种方法Encodeuri当字符串超过一个以上URI组成. encodes,一种 Uniform Resource Identifier (URI)(URI)逐一替换某些字符,描述为UTF-8编码的特点. 例子:最简单的方法就是看它们加密这些字符后生成的代码. encodeURIComponent(''!@#$%^&*(){}[]=:/;?+\''"''): 结果 !%40%23%24%25%5E%26*()%7B%7D%5B%5D%3D%3A%2F%3B%3F%2B''%22 -------------------------------------------------------------------------------- 回复:javascript 中 escape,encodeURI 区别? 什么时候适合用什么方法? escape() 方法不会加密 + 在服务器端会被解析为空格以及在表单forms区域中的spaces.基于这样的缩减方式,你应该尽可能的避免使用这种方法,二选一的话,最好的经常使用 encodeURIComponent(). escape() 不会加密: @*/+ 使用encodeURI() 比 escape() 稍专业化,它是针对URIs编码的 .一个做为querystring的反面, 属于URL的一个部分.使用这种方法是当你需要将一个字符串转换为URIs资源标识以及需要某些字符保持非encode状态.请记住, 这个'' 字符是不会进行编码的,因为它本身就包括在URIs里. encodeURI()不会加密: !@#$&*()=:/;?+'' 最后, encodeURIComponent() 方法用在大多数cases中,当你需要对一个单独URIs部件进行编码,这种方法可以加密某些特殊用于的URIs的字符,因此大部分部件可以包含在里面. 记住, '' 字符本身就包括在URIs里,所以不会此方法不会对其进行编码. encodeURIComponent() 不会加密: !*()''

PHP rawurlencode与urlencode函数的深入分析

PHP rawurlencode与urlencode函数的深入分析

本篇文章是对PHP中的rawurlencode与urlencode函数进行了详细的分析介绍,需要的朋友参考下

问题:2个函数都是针对字符串转义使其适合做文件名。该用哪个?哪个更标准?

结论:
rawurlencode遵守是94年国际标准备忘录RFC 1738,香港服务器租用,
urlencode实现的是传统做法,和上者的主要区别是对空格的转义是''+''而不是''%20''
javascript的encodeURL也是94年标准,美国空间,

而javascript的escape是另一种用"%xxx"标记unicode编码的方法。

推荐在PHP中使用用rawurlencode。弃用urlencode

样例
source:

超级无敌的人sadha sajdh数据样本sdls fhejrthcxzb.file.jpeg

PHP urlencode:
%E8%B6%85%E7%BA%A7%E6%97%A0%E6%95%8C%E7%9A%84%E4%BA%BAsadha+sajdh%E6%95%B0%E6%8D%AE%E6%A0%B7%E6%9C%ACsdls+fhejrthcxzb.file.jpeg

PHP rawurlencode:
%E8%B6%85%E7%BA%A7%E6%97%A0%E6%95%8C%E7%9A%84%E4%BA%BAsadha%20sajdh%E6%95%B0%E6%8D%AE%E6%A0%B7%E6%9C%ACsdls%20fhejrthcxzb.file.jpeg

Javascript encodeURI:
%E8%B6%85%E7%BA%A7%E6%97%A0%E6%95%8C%E7%9A%84%E4%BA%BAsadha%20sajdh%E6%95%B0%E6%8D%AE%E6%A0%B7%E6%9C%ACsdls%20fhejrthcxzb.file.jpeg

Javascript escape:
%u8D85%u7EA7%u65E0%u654C%u7684%u4EBAsadha%20sajdh%u6570%u636E%u6837%u672Csdls%20fhejrthcxzb.file.jpeg

,香港服务器租用

php urlencode vs java URLEncoder.encode

php urlencode vs java URLEncoder.encode

 

结论:urlencode 先比URLEncoder.encode多编码 “ * ” 符号,其他都保持一致

php urlencode 

  phpversion()>=5.3 will compliant with RFC 3986, while phpversion()<=5.2.7RC1 is not compliant with RFC 3986.

  参考 RFC3896 方式编码

  

返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+)。
此编码与 WWW 表单 POST 数据的编码方式是一样的,同时与 application/x-www-form-urlencoded 的媒体类型编码方式一样。
由于历史原因,此编码在将空格编码为加号(+)方面与 » RFC3896 编码(参见 rawurlencode())不同。

 

php并没有完全按照 rfc3896编码,符号【~】在标准中是不用编码,但是他也编码了。

 

所以最终的未编码的字符列表为 [-], [_], [.],如同其文档中描述的一样

java URLEncoder.encode

  参考 RFC2396 方式编码

  但是由于ie浏览器编码了除  "-", "_", ".", "*" 之外的字符,java采用了和IE一样的编码列表,

  所以最终的未编码的字符列表为 [-], [_], [.], [*]

  

The list of characters that are not encoded has been
determined as follows:

RFC 2396 states:
-----
Data characters that are allowed in a URI but do not have a
reserved purpose are called unreserved.  These include upper
and lower case letters, decimal digits, and a limited set of
punctuation marks and symbols.

unreserved  = alphanum | mark

mark        = "-" | "_" | "." | "!" | "~" | "*" | "''" | "(" | ")"

Unreserved characters can be escaped without changing the
semantics of the URI, but this should not be done unless the
URI is being used in a context that does not allow the
unescaped character to appear.
-----

It appears that both Netscape and Internet Explorer escape
all special characters from this list with the exception
of "-", "_", ".", "*". While it is not clear why they are
escaping the other characters, perhaps it is safest to
assume that there might be contexts in which the others
are unsafe if not escaped. Therefore, we will use the same
list. It is also noteworthy that this is consistent with
O''Reilly''s "HTML: The Definitive Guide" (page 164).

As a last note, Intenet Explorer does not encode the "@"
character which is clearly not unreserved according to the
RFC. We are being consistent with the RFC in this matter,
as is Netscape.

 

History of related RFCs:

RFC 1738 section 2.2
only alphanumerics, the special characters "$-_.+!*''(),", and
reserved characters used for their reserved purposes may be used
unencoded within a URL.

RFC 2396 section 2.3
unreserved = alphanum | mark
mark = "-" | "_" | "." | "!" | "~" | "*" | "''" | "(" | ")"

RFC 2732 section 3
(3) Add "[" and "]" to the set of ''reserved'' characters:

RFC 3986 section 2.3
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"

RFC 3987 section 2.2
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"

 

今天关于urlencode值数组url数组参数的介绍到此结束,谢谢您的阅读,有关java中URLEncoder.encode与URLDecoder.decode处理url特殊参数的方法、JS escape urlencodeComponent urlencode 区别、PHP rawurlencode与urlencode函数的深入分析、php urlencode vs java URLEncoder.encode等更多相关知识的信息可以在本站进行查询。

本文标签: