Is a URL important?

1

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

I realized that when I use some language methods for Uri , it 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 WebUtility.UrlDecode() , and it returns to be as before.

But why does he do this? Do you have any importance in encoding the URL?

    
asked by anonymous 23.01.2018 / 06:51

1 answer

8

The importance is to work right:

Comparing with a programming language: this is the same as coding quotes inside a string. For the quotation marks to work, you can not have a string like that in a conventional code:

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

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

Same thing in your example:

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

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

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

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

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


Generally you give the encode 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 code the name of the parameters too, but for the sake of sanity (yours and the application) the usual thing is to make names legible and unambiguous.

In summary, you convert depending solely and exclusively depending on the intent of your application. Is it a data that goes in a parameter? Need to encode. Is it another part of the address? This is already the context of your application.

What you are not going to do is to encode the entire URL, otherwise you will not touch parts you do not need. For example, if you give the encode at the start address of your example, it will look like this (and will not work correctly):

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