Uri Unsafe Characters problem – Image Upload in Windows Phone 7

As of the title may suggest, you might have an impression at the very beginning, that why the heck someone would possibly think of uploading images to Windows Phone 7 at this era of Windows Phone (it’s been almost a year since Windows Phone 8 is out there in the market, right?). The thing is, actually it was the requirement of my employers. Now without further ado, let’s jump over to our main talk.

My scenario: we have been working on a socializing app for quite a long time, me with my partner Ashekul Islam Khan was appointed on the WP client side. I was trying to upload photo from my WP client to a Java server. If you have a problem uploading image in WP and Bing it, I guess there would be tons of search results. More or less we have tried to use all of them, but the disappointing fact was none of them seemed to work.

First thing first the byte representation of these two platforms are not the same (in c# it’s unsigned, while in java it’s signed). So we decided to send data over String format. We tried to use base64 strings. We tried image upload from WP client to .NET server, it worked just fine. But approaching different server had some problems. The string base64 converted data we sent over an Uri and the server side received data seemed exactly same at a first glance, but after a while we found there are actually three characters that are not exactly the same on both sides. That’s when we found the fact of Uri unsafe characters (Uri strings as defined by RFC 3986). For that we needed to do something extra, encode and put some Uri safe characters from client side and do the exact opposite to get the bas64 string back on the server side.

Client side encoded data:

static string base64urlencode(byte[] arg)
{
string s = Convert.ToBase64String(arg); // Regular base64 encoder
s = s.Split('=')[0]; // Remove any trailing '='s
s = s.Replace('+', '-'); // 62nd char of encoding
s = s.Replace('/', '_'); // 63rd char of encoding
return s;
}</code>

Server side decoded data:

<code>static byte[] base64urldecode(string arg)
{
string s = arg;
s = s.Replace('-', '+'); // 62nd char of encoding
s = s.Replace('_', '/'); // 63rd char of encoding
switch (s.Length % 4) // Pad with trailing '='s
{
case 0: break; // No pad chars in this case
case 2: s += "=="; break; // Two pad chars
case 3: s += "="; break; // One pad char
}
return Convert.FromBase64String(s); // Standard base64 decoder
}

And you will need a PhotoChooserTask  to select and get photo data. We have also implemented it on a php server and its working just fine (yes, of course you have to write some script on the php server side J). I will get back to you guys with complete codes. For the time being, its TaDa.

Now thanks giving: it’s been a real honor that Swagata let me write something in this blog. Poitta, I owe you some candy! 🙂

Advertisements

One thought on “Uri Unsafe Characters problem – Image Upload in Windows Phone 7

  1. Though I’m not a guy of Windos Phone but I love this post for its simplicity.

    Wish you best Mesbah.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s