views:

431

answers:

4
+2  Q: 

URL-encode a URL

Is there a .NET encoding method I could use to encode a URL to be passed within a URL parameter?

For example say I have:

url_of_interest = "http://asdf.asdf/asdf.htm"

and I want to include this as one (1) URL form parameter when I do an upload to a web-application:

http://mywebservice/upload?url=<<encoded URL here>>

+8  A: 

System.Web.HttpUtility.UrlEncode

ChaosPandion
+1  A: 

HttpUtility.UrlEncode

The UrlEncode() method can be used to encode the entire URL, including query-string values. If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. URL encoding converts characters that are not allowed in a URL into character-entity equivalents; URL decoding reverses the encoding. For example, when embedded in a block of text to be transmitted in a URL, the characters < and > are encoded as %3c and %3e.

cxfx
+2  A: 

HttpServerUtility.UrlEncode should do the trick:

http://msdn.microsoft.com/en-us/library/zttxte6w.aspx

pygorex1
I had to down vote you because you cannot create an instance of this class.
ChaosPandion
Why does it matter that an instance cannot be created? An instance is provided for you if you're running in ASP.NET.
John Saunders
He does not specify anywhere that he is using ASP.NET.
ChaosPandion
@ChaosPandion Feeling a little anal-rentenative today, are we? The question did not exclude ASP.NET so the answer is valid option.
pygorex1
@John, I have to side with ChaosPandion on this one. I came to this question looking for answers on specifically how to do this *outside* of the ASP.NET realm.
Tomas Lycken
+1  A: 

You should use MS Anti XSS library's

AntiXss.UrlEncode method

The AntiXSS library can be downloaded from the following location

http://www.microsoft.com/downloads/details.aspx?familyid=051EE83C-5CCF-48ED-8463-02F56A6BFC09&amp;displaylang=en

nitroxn