3

Possible Duplicate:
.net UrlEncode - lowercase problem

I'm using the HttpUtility.UrlEncode method to encode a string for me. The problem is that the letters in the encoded places are in lower case for example:

a colon(:) becomes %3a rather than %3A.

Not so much of an issue until I come to encrypt this string. The end result I want looks like this.

zRvo7cHxTHCyqc66cRT7AD%2BOJII%3D

If I use capital letters I get this

zRvo7cHxTHCyqc66cRT7AD+OJII=

which is correct, but if I use lower case letters (ie use UrlEncode rather than a static string) I get this

b6qk+x9zpFaUD6GZFe7o1PnqXlM=

Which is obviously not the string I want. Is there a simple way to make the encoded characters capital without reinventing the wheel of UrlEncoding?

Thnaks

Community
  • 1
  • 1
James Hay
  • 7,115
  • 6
  • 33
  • 57
  • Check this one out, should solve your problem: http://stackoverflow.com/questions/918019/net-urlencode-lowercase-problem – Matt Mar 13 '12 at 03:34
  • Yup that's the one, solved my problem! Can I mark this as duplicate or something? – James Hay Mar 13 '12 at 03:37
  • @James Hay: Yes, you can vote to close your own questions as duplicates, and/or flag them for a speedy closure. I've done that for you now. – BoltClock Mar 23 '12 at 22:52

1 Answers1

3

Sure, CAP the string before you encode it. Encoding is generally a one way street based on literal character values so it is no wonder result it different. I do wonder though, what type of value are you using this for? There most certainly is a better way to handle the type of data you're encoding.

An addition to the linked duplicate:

public static string UpperCaseUrlEncode(this string s)
{
    char[] temp = HttpUtility.UrlEncode(s).ToCharArray();
    for (int i = 0; i < temp.Length - 2; i++)
    {
        if (temp[i] == '%')
        {
            temp[i + 1] = char.ToUpper(temp[i + 1]);
            temp[i + 2] = char.ToUpper(temp[i + 2]);
        }
    }
    return new string(temp);
}

Turning it into an extension method allows any string variable to be UPPER CASE URL Encoded.

M.Babcock
  • 18,753
  • 6
  • 54
  • 84