Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Encoding with capital letters [duplicate]

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

like image 281
James Hay Avatar asked Nov 16 '25 15:11

James Hay


1 Answers

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.

like image 128
M.Babcock Avatar answered Nov 18 '25 06:11

M.Babcock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!