C # mvc2 encode url

I am trying to encode a URL using the code below:

var encodedUrl = HttpUtility.UrlEncode("http://www.example.com"); var decodedUrl = HttpUtility.UrlDecode("http%3A%2F%2Fwww%2Eexample%2Ecom%2F"); 

I am working with Google webmaster tools api and this api expects a url as shown in decodedUrl variable above. Each character is encoded there.

When I use the httputility encoding function, I get the following result:

 http%3a%2f%2fwww.example.com 

How can I use an encoding variable so that every character in the URL is encoded?

+4
source share
2 answers

I am sure that HtmlUtility and AntiXss (another MS URL encoding tool) will not help here. A "." in the URL is considered valid and therefore it does not need to be encoded.

I think you will have to publish your own encoded string to further encode other characters that are not valid in the Google webmasters APIs.

i.e. do something like that ...

 var encodedUrl = HttpUtility.UrlEncode("http://www.example.com") .Replace(".", "%2E"); 

... assuming "." this is the only character you have encountered.

+4
source

A period is not a reserved character in the URL, so it will not be encoded. See this question and answer for an elegant solution.

+4
source

All Articles