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.asd开发者_开发知识库f/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>>
System.Web.HttpUtility.UrlEncode
HttpServerUtility.UrlEncode should do the trick:
http://msdn.microsoft.com/en-us/library/zttxte6w.aspx
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.
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&displaylang=en
As System.Web is being decommissioned from a modern OWIN based application, the alternative solution to HttpUtility.UrlEncode
should be WebUtility.UrlEncode
精彩评论