0

HttpUtility.UrlEncode("abc : 123") produces abc+%3a+123 but I need it to produce abc+%3A+123 (notice the uppercase A.

Is there a way to have UrlEncode output uppercase hex characters?

I don't want to simply call .ToUpper() on the entire string because I need abc to stay lowercase.

Joe Phillips
  • 49,743
  • 32
  • 103
  • 159
  • This post may help (https://stackoverflow.com/questions/575440/url-encoding-using-c-sharp), `HttpUtility.UrlEncode` will transform a colon into %3a lowercase, if you need the upper case A, I believe you may need to use a different utility for your encoding, the provided post shows the output of encoding from different C# classes. I believe from the provided tables in the post that you may want to use `WebUtility.UrlEncode(string)` – Ryan Wilson Aug 13 '18 at 19:35

2 Answers2

2

I believe I've found the answer from the 2nd answer in this question: https://stackoverflow.com/a/1148326/20471

The solution is to use Uri.EscapeDataString because it uses %20 for spaces instead of + and also uppercases the hex replacements.

Joe Phillips
  • 49,743
  • 32
  • 103
  • 159
1

This is not supported out of the box, but you can do it yourself:

private static string UrlEncodeUpperCase(string stringToEncode)
{
    var reg = new Regex(@"%[a-f0-9]{2}");
    stringToEncode = HttpUtility.UrlEncode(stringToEncode);
    return reg.Replace(stringToEncode, m => m.Value.ToUpperInvariant());
}

Keep in mind that [RFC 3986][1] explicitly mentions that uppercase and lowercase are equivalent.

Mel Gerats
  • 2,234
  • 1
  • 16
  • 33