Search code examples
c#.neturlhtml-entitieshtml-encode

WebUtility.HtmlEncode doesn't convert `:`


I do a request to a REST web service. The web service accepts a date parameter in the following format:

?date=0001-01-01T00:00:00Z

The web service only accepts if the colons in the parameter are converted to %3A as follows

?date=0001-01-01T00%3A00%3A00Z

I believe I could use WebUtility.HtmlEncode() for this conversion. But after using it, the colons aren't parsed to %3A.

Am I missing something? How can I get the parameter in the following format?

?date=0001-01-01T00%3A00%3A00Z

I already lost a lot of time for this simple thing, I don't want to write code for doing only this... There must be some .NET library which already can do this.


Solution

  • : is a valid character in HTML, which is why HtmlEncode isn't touching it. You're dealing with a URL, so you need UrlEncode:

    var url = WebUtility.UrlEncode(":"); //%3A