kmoonki / oauth

Automatically exported from code.google.com/p/oauth
0 stars 0 forks source link

C# Library Fails to encode multibyte chars #152

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Probably similar to Issue 126. The UrlEncode method in OAuthBase.cs just 
appends a hex encoding of the character. For multibyte chars you need to 
hex encode each byte of the character instead. 

My version of this method is (where NoEncodeChars is the list of characters 
that do not require encoding):

public static string OAuthUrlEncode(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return string.Empty;
            }
            else
            {
                StringBuilder sb = new StringBuilder(s.Length);

                for (int i = 0; i < s.Length; i++)
                {
                    if (NoEncodeChars.IndexOf(s[i]) == -1)
                    {
                        // character needs encoding
                        byte[] characterBytes = 
Encoding.UTF8.GetBytes(s[i].ToString());
                        for (int b = 0; b < characterBytes.Length; b++)
                        {
                            sb.AppendFormat(CultureInfo.InvariantCulture,
                            "%{0:X2}",
                            characterBytes[b]);
                        }
                    }
                    else
                    {
                        // character is allowed
                        sb.Append(s[i]);
                    }
                }

                return sb.ToString();
            }
        }

Original issue reported on code.google.com by robert.e...@gmail.com on 4 Apr 2010 at 4:30

GoogleCodeExporter commented 9 years ago

Original comment by morten.f...@gmail.com on 12 Jun 2010 at 8:47

GoogleCodeExporter commented 9 years ago
Yes!  I think this change should be merged.  Solved the problem for us.

Original comment by t...@wishpot.com on 3 Dec 2011 at 5:17