zoukankan      html  css  js  c++  java
  • When to encode space to plus (+) or %20?

    When to encode space to plus (+) or %20?

    回答1

    + means a space only in application/x-www-form-urlencoded content, such as the query part of a URL:

    http://www.example.com/path/foo+bar/path?query+name=query+value
    

    In this URL, the parameter name is query name with a space and the value is query value with a space, but the folder name in the path is literally foo+bar, not foo bar.

    %20 is a valid way to encode a space in either of these contexts. So if you need to URL-encode a string for inclusion in part of a URL, it is always safe to replace spaces with %20 and pluses with %2B. This is what eg. encodeURIComponent() does in JavaScript. Unfortunately it's not what urlencode does in PHP (rawurlencode is safer).

    See Also HTML 4.01 Specification application/x-www-form-urlencoded

    回答2

    So, the answers here are all a bit incomplete. The use of a '%20' to encode a space in URLs is explicitly defined in RFC3986, which defines how a URI is built. There is no mention in this specification of using a '+' for encoding spaces - if you go solely by this specification, a space must be encoded as '%20'.

    The mention of using '+' for encoding spaces comes from the various incarnations of the HTML specification - specifically in the section describing content type 'application/x-www-form-urlencoded'. This is used for posting form data.

    Now, the HTML 2.0 Specification (RFC1866) explicitly said, in section 8.2.2, that the Query part of a GET request's URL string should be encoded as 'application/x-www-form-urlencoded'. This, in theory, suggests that it's legal to use a '+' in the URL in the query string (after the '?').

    But... does it really? Remember, HTML is itself a content specification, and URLs with query strings can be used with content other than HTML. Further, while the later versions of the HTML spec continue to define '+' as legal in 'application/x-www-form-urlencoded' content, they completely omit the part saying that GET request query strings are defined as that type. There is, in fact, no mention whatsoever about the query string encoding in anything after the HTML 2.0 spec.

    Which leaves us with the question - is it valid? Certainly there's a LOT of legacy code which supports '+' in query strings, and a lot of code which generates it as well. So odds are good you won't break if you use '+'. (And, in fact, I did all the research on this recently because I discovered a major site which failed to accept '%20' in a GET query as a space. They actually failed to decode ANY percent encoded character. So the service you're using may be relevant as well.)

    But from a pure reading of the specifications, without the language from the HTML 2.0 specification carried over into later versions, URLs are covered entirely by RFC3986, which means spaces ought to be converted to '%20'. And definitely that should be the case if you are requesting anything other than an HTML document.

  • 相关阅读:
    OpenWrt配置绿联的usb转Ethernet网口驱动
    SQL_wm_concat函数实验:实现字段合并
    BingMap频繁Add Pushpin和Delete Pushpin会导致内存泄露
    比較C++和Java 二
    【JAVASE】Java同一时候抛出多个异常
    uva 1463
    Android 撕衣服(刮刮乐游戏)
    轻松掌握一致性哈希算法
    Oracle之sql语句优化
    Eclipse导出Library
  • 原文地址:https://www.cnblogs.com/chucklu/p/15220204.html
Copyright © 2011-2022 走看看