Groups | Blog | Home
all groups > dotnet general > february 2008 >

dotnet general : Escape characters in URL in vb.net


graphicsxp@googlemail.com
2/14/2008 9:08:07 AM
Hi,
I've got a url that looks like that :

http://www.myurl.net/Drops 10% - News.doc

Obviously to be properly interpreted by browsers, special characters
should be converted (% -> %25 and spaces to %20)

Unfortunately I can't seem to find the right function in vb.net to do
that.... I've tried Server.UrlEncode which converts to a lot of crap,
and HttpUtility.UrlEncode which doesn't do it properly either...

Anyone knows how to do that ? Can't be that hard :(

Jon Skeet [C# MVP]
2/14/2008 6:08:13 PM
[quoted text, click to view]

What makes you think they're not working properly? Bear in mind that
you shouldn't be passing in the first part of the URL - just the "Drops
10% - News.doc" into UrlEncode.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
graphicsxp@googlemail.com
2/15/2008 1:22:33 AM
[quoted text, click to view]

HttpUtility.UrlEncode encodes the url as : Drops+10%25+-+Gulf
+News.doc

That doesn't work in IE or Firefox. Why does it encode spaces with
pluses ??! I can understand in the case of a querystring, but here
graphicsxp@googlemail.com
2/15/2008 2:25:33 AM
[quoted text, click to view]

Well considering the files are sent to me by clients I can't really
ask them not to put this kind of signs in their filenames.... although
I would not to that myself.
Yeah I've noticed that too with URLPathEncode.... I think it's a bug
as well, but considering .net 2.0 has been around for 3 years, I just
can't believe no one has come across that before and fixed the issue !
Anyway I can't wait for Microsoft to make a move so I;ve used a good
Jon Skeet [C# MVP]
2/15/2008 9:34:54 AM
[quoted text, click to view]

Hmm. I tried using HttpUtility.UrlPathEncode instead, as it's a path
that you need to encode.

A quick test shows that working slightly better:
Drops10%%20-%20News.doc

.... but it's not encoding the original % now. I've no idea why.

Admittedly I'd try to avoid having such characters in paths to start
with, but that *looks* like a bug to me.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
AddThis Social Bookmark Button