Character Encoding Error C# .NET

180 views
Skip to first unread message

Ryan Bell

unread,
Dec 30, 2010, 10:53:08 AM12/30/10
to Twitter Development Talk
Hi,

A few months ago, Twitter stopped accepting messages from our
application that contained special language characters. We assume
that the API is now very strict on character encoding. We have
analyzed our code and we are following the best practices of character
encoding into utf8. Just before we send the message to the API, we
encode it into utf8, then place the text into the url and send it. We
receive back 403 errors for any messages that contain special
characters (like umlauts).

We've tried everything that we can think of. Can you please send us
an example of how to properly encode the message? We are using c#.

Any help is greatly appreciated.

Thank You,

Ryan Bell

Tom van der Woerdt

unread,
Dec 30, 2010, 11:26:05 AM12/30/10
to twitter-deve...@googlegroups.com
403 errors => Unauthorized. I'd assume that the problem is in your Base
String.

Try recreating this example. Post a Tweet with
"setting up my twitter 私のさえずりを設定する".

* Put "status=setting%20up%20my%20twitter%20私のさえずりを設定する" in
the POST body.
* The Base String *must* have (case-sensitive, etc) :
status%3Dsetting%2520up%2520my%2520twitter%2520%25E7%25A7%2581%25E3%2581%25AE%25E3%2581%2595%25E3%2581%2588%25E3%2581%259A%25E3%2582%258A%25E3%2582%2592%25E8%25A8%25AD%25E5%25AE%259A%25E3%2581%2599%25E3%2582%258B
in it.

Please confirm that you are getting the same values. If you do not, you
should look at how you encode UTF8 data.

Tom

@Red_Eyes

unread,
Jan 2, 2011, 8:49:06 PM1/2/11
to twitter-deve...@googlegroups.com
I had this problem a while back after switching to oAuth and ended up writing my own encoding function which works fine.
 
Here are the essential snippets!
 
            string oAuthUnreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
            StringBuilder sb = new StringBuilder();
            Encoding uTF8Encoding = Encoding.UTF8;
 
                   foreach (char thisChar in inStr)
                    {
                        if (oAuthUnreservedChars.IndexOf(thisChar) != -1)
                        {
                            sb.Append(thisChar);
                        }
                        else
                        {
                            byte[] bytes = uTF8Encoding.GetBytes(thisChar.ToString());
                            foreach (byte b in bytes)
                            {
                                sb.Append('%' + String.Format("{0:X2}", b));
                            }
                        }
                    }
Hope that works for you!
 
Regards
 
Paul
Reply all
Reply to author
Forward
0 new messages