Is coding a URL important?

Asked

Viewed 301 times

1

Most languages have methods to encode and decode a URL, as in the case of C# System.Net.WebUtility.UrlEncode(string value) and System.Net.WebUtility.UrlDecode(string encodedValue).

I realized that when I use some language methods to Uri, he supposedly uses this encoding.

For example it encodes a url like this:
http://example.com/index?param1=abc&url=example.com/page/blabla
for:
http://example.com/index?param1=abc&url=example.com%2page%2blabla

I can decode without problems with the WebUtility.UrlDecode(), and goes back to being like before.

But why does he do it? It has some importance in encoding the URL?

1 answer

8


The importance is to work right:

Comparing to a programming language: it’s the same thing as encoding quotes inside a string. For the interpretation of quotation marks to work, you cannot have a string like this in a conventional code:

texto = "olá, José "Jones" da silva"

In this case you have to encode the quotes of the "Jones" so that they are not confused with the closing quotes.

Same thing in your example:

 http://example.com/index?param1=abc&url=example.com/page/blabla
      ^^           ^                                ^    ^

From the above shape, bars are parts of the address as a whole.

Already, so the bars are only part of the parameter url:

http://example.com/index?param1=abc&url=example.com%2Fpage%2Fblabla
                                                   ^^^    ^^^

the %2f represents the character / after coding, and will not be confused as part of the address (similarly the character & in the given example would be understood as parameter separator, so it would have to be converted to %23, as well as the sign = would have to be converted to %3d, and so on).


Usually you give the Encounter only in values:

Whether in variables or parts separated by / when applicable.

http://example.com/index?param1=abc&url=example.com%2Fpage%2Fblabla
                   ^^^^^        ^^^     ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Depending on the context you could even encode the name of the parameters as well, but for the sake of sanity (yours and the application) the usual is to put names readable and unambiguous.

In short, you convert depending solely and exclusively on the intention of your application. Is a data going in a parameter? You need to encode. Is this another part of the address? It’s gone from the context of your application.

What you won’t do is give Find the whole URL, otherwise you’ll mess with parts you don’t need. For example, if you give the Encode in the initial address of your example, it will look like this (and it will not work properly):

http%3A%2F%2Fexample.com%2Findex%3Fparam1%3Dabc%26url%3Dexample.com%252Fpage%252Fblabla%0D%0A

Browser other questions tagged

You are not signed in. Login or sign up in order to post.