Why do you need to encode URLs?

后端 未结 5 1218
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 04:16

Why do you need to encode urls? Is there a good reason why you have to change every space in the GET data to %20?

相关标签:
5条回答
  • 2020-12-08 04:31

    Because some characters have special meanings.

    For instance, in a query string, the ampersand (&) is used as a separator between key-value pairs. If you were to put an ampersand into one of those values, it would look like the separator between the end of a value and the beginning of the next key. So for special characters like this, we use percent encoding so that we can be sure that the data is unambiguously encoded.

    0 讨论(0)
  • 2020-12-08 04:31
    • originally older browsers could get confused by the spaces (not really an issue anymore).
    • now, if someone copies the url to send as a link - the space can break the hyperlink - ie

    Hey! Check out this derping cat playing a piano!

    http://www.mysite.com/?video=funny cat plays piano.
    

    See how the link breaks?

    Now look at this:

    http://www.mysite.com/?video=funny%20cat%20plays%20piano.
    
    0 讨论(0)
  • 2020-12-08 04:39

    From RFC 2936, section 2.4.3:

    The space character is excluded because significant spaces may disappear and insignificant spaces may be introduced when URI are transcribed or typeset or subjected to the treatment of word- processing programs. Whitespace is also used to delimit URI in many contexts.

    0 讨论(0)
  • 2020-12-08 04:39

    Let's break down your question.
    Why do you need to encode URL?
    A URL is composed of only a limited number of characters and those are digits(0-9), letters(A-Z, a-z), and a few special characters("-", ".", "_", "~").
    So does it mean that we cannot use any other character?
    The answer to this question is "YES". But wait a minute, there is a hack and the hack is URL Encoding or Perchantage Encoding. So if you want to transmit any character which is not a member of the above mentioned (digits, letters, and special chars), then we need to encode them. And that is why we need to encode "space" as "%20".
    OK? Is this enough for URL encoding? No this is not enough, there's a lot about URL encoding but here, I'm not gonna make it a pretty big, boring technical answer. But If you want to know more, then you can read it from here: https://www.urlencoder.io/learn/ (Credit goes to this writer)

    0 讨论(0)
  • 2020-12-08 04:39

    Well, you do so because every different browsers knows how the string that makes up the URL is encoded. converting the space to %20, etc makes that URL/URI portable. It could be latin-1 it could be unicode. It needs normalized to something that is understood universally. Take a look at rfc3986 http://tools.ietf.org/html/rfc3986#section-2.1

    0 讨论(0)
提交回复
热议问题