Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape url encoding?

I am creating a link that creates URL parameters that contains links with URL parameters. The issue is that I have a link like this

http://mydomain/_layouts/test/MyLinksEdit.aspx?auto=true&source=
    http://vtss-sp2010hh:8088/AdminReports/helloworld.aspx?pdfid=193
&url=http://vtss-sp2010hh:8088/AdminReports/helloworld.aspx?pdfid=193%26pdfname=5.6%20Upgrade
&title=5.6 Upgrade

This link goes to a bookmark adding page where it reads these parameters.

auto is wheather to read the following parameters or not

source is where to go after you finish adding or cancelling

url is the bookmark link

title is the name of the bookmark

The values of url and title get entered into 2 fields. Then the user has to click save or cancel. The problem is when the bookmark page enters the values into the field, it will decode them. Then if you try to save, it will won't let you save because the pdfname value in the url value has a space in it. It needs the link to not have any spaces. So basically, I want it so that after it enters it in the field, it will still be a %20 instead of a space.

There isn't a problem with source, auto, or title, just the url...

Is there a way to solve this? Like maybe a special escape character I can use for the %20?

Note: I cannot modify the bookmark page.

I am using c#/asp.net to create the link and go to it.

Thanks

like image 351
omega Avatar asked Jun 21 '13 18:06

omega


People also ask

What does %20 do in URL?

A space is assigned number 32, which is 20 in hexadecimal. When you see “%20,” it represents a space in an encoded URL, for example, http://www.example.com/products%20and%20services.html.

What does %2 mean in a URL?

The % indicates an escaped character. It's a hexadecimal number that follows in the next two characters. In your example that is %2C , which is the hexadecimal number for the comma. Unescaped that becomes asset=travel,car,house,business.


3 Answers

Since .NET Framework 4.5 you can use WebUtility.UrlEncode.

It resides in System.dll, so it does not require any additional references.

It properly escapes characters for URLs, unlike Uri.EscapeUriString

It does not have any limits on the length of the string, unlike Uri.EscapeDataString, so it can be used for POST requests

 System.Net.WebUtility.UrlEncode(urlText) 

Another option is

 System.Uri.EscapeDataString()  
like image 182
ericdc Avatar answered Sep 22 '22 05:09

ericdc


Uri.EscapeDataString() and Uri.UnescapeDataString() are safe comparing to UrlEncode/UrlDecode methods and does not convert plus characters into spaces when decoding.

Some details from another user: http://geekswithblogs.net/mikehuguet/archive/2009/08/16/134123.aspx

like image 38
mmosoll Avatar answered Sep 18 '22 05:09

mmosoll


Just use HttpUtilty's UrlEncode method right before you hand off the url;

 string encoded = HttpUtility.UrlEncode(url);
like image 22
evanmcdonnal Avatar answered Sep 18 '22 05:09

evanmcdonnal