数组究竟是如何进行 urlencode 编码的?

发布于 2024-09-04 07:41:17 字数 68 浏览 1 评论 0原文

许多语言允许通过 url 传递一组值。由于各种原因,我需要直接手动构建 url。值数组如何进行 urlencode 编码?

Many languages allow one to pass an array of values through the url. I need to , for various reasons, directly construct the url by hand. How is an array of values urlencoded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2024-09-11 07:41:17

看起来像 MIME-Type 形式的内容:application/x-www-form-urlencoded

这是默认内容类型。使用此内容类型提交的表单必须按如下方式编码:

  1. 控件名称和值被转义。空格字符被替换为 +,然后保留字符被转义,如 [RFC1738] 第 2.2 节中所述:非字母数字字符被替换为 %HH(百分号)两个十六进制数字代表该字符的 ASCII 码。换行符表示为“CR LF”对(即 %0D%0A)。
  2. 控件名称/值按照它们在文档中出现的顺序列出。名称与值之间用 = 分隔,名称/值对之间用 & 分隔。

用于 POST。要为 GET 执行此操作,您必须在 URL 后面附加一个 ?,其余部分几乎相同。在评论中,mdma 指出,URL 不能包含用于空格字符的 +。请改用 %20

因此,值数组:

http://localhost/someapp/?0=zero&1=valueone%20withspace&2=etc&3=etc

库中通常有一些功能可以为您执行 URL 编码(第 1 点)。第二点很容易实现,方法是循环数组、构建字符串、附加索引、=、URL 编码值,如果不是最后一个条目,则添加 &

It looks like the content in the form of MIME-Type: application/x-www-form-urlencoded.

This is the default content type. Forms submitted with this content type must be encoded as follows:

  1. Control names and values are escaped. Space characters are replaced by +, and then reserved characters are escaped as described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by %HH, a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., %0D%0A).
  2. The control names/values are listed in the order they appear in the document. The name is separated from the value by = and name/value pairs are separated from each other by &.

Which is used for the POST. To do it for the GET, you'll have to append a ? after your URL, and the rest is almost equal. In the comments, mdma states, that the URL may not contain a + for a space character. Instead use %20.

So an array of values:

http://localhost/someapp/?0=zero&1=valueone%20withspace&2=etc&3=etc

Often there is some functionality in libraries that will do the URL encoding for you (point 1). Point two is easily implementable by looping over your array, building the string, appending the index, =, the URL encoded value and when it's not the last entry an &.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文