4

i'm trying to encode an url with the code below;

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

I'm working with the google webmaster tools api and this api expects an URL as shown in the decodedUrl variable above. Every single character is encoded there.

When i use the httputility encode function i get the following result;

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

How can i use the encoding variable in such a way that every character in the url is encoded?

asked Oct 8, 2010 at 13:19

2 Answers 2

4

I'm pretty sure that HtmlUtility and AntiXss (another MS tool for encoding urls) aren't going to help here. A "." in a url is considered valid and so doesn't need to be encoded.

I think you're going to have to post-process your encoded string to further encode other characters that are not valid within teh google webmaster tools API.

i.e. do something like this...

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

... assuming that "." is the only character you're having problems with.

answered Oct 8, 2010 at 13:43
3
  • Wrote an extension method to replace the characters and it works like a charm. Thanks! Commented Oct 8, 2010 at 14:03
  • Cool. an extension method will certainly tidy the code up nicely. Commented Oct 8, 2010 at 15:49
  • Totally agree! It definately cleanes up the code and it's easy maintainable. We use it a lot over here. Lots of projects have a Toolbox included with helpers and extensions methodes which can used overall Commented Oct 22, 2010 at 13:48
4

The period is not a reserved character in a URL, so it won't be encoded. See this question and answer for an elegant solution.

answered Oct 8, 2010 at 13:58
1
  • +1 for the good comment, i'll keep this in mind for final deployment Commented Oct 8, 2010 at 14:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.