Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

WP7 Tip: Always Encode Urls for the WebBrowserTask

DZone's Guide to

WP7 Tip: Always Encode Urls for the WebBrowserTask

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

Exactly as the title says, remember to encode the url you want to open in the external browser via the WebBrowserTask, always.

This is probably the first of a series of posts I’m going to write about some problems I found while developing FeedTso, a feed news reader application for WP7, of course with the solution.

But let’s go back to the problem.

How to open a url in the WP7 web browser from your application

Well, this is easy:

WebBrowserTask task = new WebBrowserTask();
task.URL = "http://feedtso.com/";
task.Show();

Just create the task, set the URL (remember, it is a string, not a Uri) and call the Show method.

Adding parameters to the mix

But with more complex urls, with parameters or when the parameter is another url, things go wrong.

In my news reader I let users open the post in the external browser also in text-only mode, using a third party service from Instapaper, called mobilizer.

To make it work you have to call the url http://www.instapaper.com/m and add a parameter named u with the url of the page you want to be “mobilized”.

But in order to add it as parameter, the url has to be encoded to make it legal as uri: so if you want to see the text-only version of the features of FeedTso you have to call the following url (which is urlencoded):

http://www.instapaper.com/m?u=http%3A%2F%2Ffeedtso.com%2Ffeatures.html

This works fine if typed manually into the address bar of a browser, but if I send this url to the web browser task, I see the decoded url, which is invalid and thus generates a bad response from the server.

I looked at the URL property on MSDN and it says:

 

Remark:
URLs that contain special characters must be escaped before being assigned to the URL property. Use the Uri.EscapeDataString method to escape the URL string.


I thought it was only applying to the normal url encoding you do if your parameters have special characters. But apparently it means that everything has to be UrlEncoded, also after having already encoded the parameter.

Probably the web browser task always decode the url before sending it to the browser, and in my case it was decoding also the encoding of the url parameters and was making the navigation fail.

How to navigate to a Uri with parameters?

The gotcha is that you have to encode it again before passing the url to the WebBrowserTask.

string serviceUrl = "http://www.instapaper.com/m?u=";
string destinationUrl = "http://feedtso.com/features.html";
string escapedUrl = serviceUrl + Uri.EscapeDataString(destinationUrl);
WebBrowserTask task = new WebBrowserTask();
task.URL = Uri.EscapeDataString(escapedUrl);
task.Show();

Encoding it the second time would generate an invalid url:

 

http%3A%2F%2Fwww.instapaper.com%2Fm%3Fu%3Dhttp%253A%252F%252Ffeedtso.com%252Ffeatures.html

 

But apparently that’s the only way to have the WebBrowserTask behave correctly.

Wrapping up

This also seems like one of the gotcha WP7 developers might need to live with, as I don’t think Microsoft will ever be able to fix this as by doing so it will break all the applications that already workaround that strange behavior.




Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Published at DZone with permission of Simone Chiaretta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}