Tag: Networking

The Starbucks Wi-Fi Login Scenario: Why does this crash?

University of Arizona Starbucks

Developer Scenario: You have written your app to make sure there is a network connection prior to downloading data and populating the app. You have thoroughly tested you app at work with wired internet, in your car with 2, 3, 4 and no G (no connection) cellular connections. You also diligently tested your app with your home Wi-Fi and it works GREAT! You publish your app to the marketplace and find you are getting a few bad reviews (app is crashing). In reviewing your analytics, you see that these crashes are occurring when connected to Wi-Fi. You take your app to your favorite internet café and notice that the Windows Phone navigates to the web browser after connecting to the Wi-Fi access point. Wondering what will happen if you do NOT agree to the ‘terms-of-service’ you fire up your app and BOOM it crashes! 🙁 You start your debugger and find that your app is in fact downloading data, but instead of being in XML format:

<?xml version="1.0" encoding="utf-8"?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
  </food>
</breakfast_menu>

its in the form of:

<!DOCTYPE html>
<html class=" " lang="en-US">
  <head>
    <title>Starbucks Coffee Company</title>
. . .
Your app proceeds to deserialize the incoming data and quickly crashes since it is not in the expected format.

Detecting and Mitigating this scenario

The best way to protect against this scenario, is to validate the data. You don’t need to validate the entire response stream, just enough to know that its XML (or JSON, or whatever) and NOT HTML. You can the very quickly check a very small portion of the returned data to determine if the data you are receiving is the expected data. You can also validate against a response header. Here are the two methods (assume you are downloading xml):

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)

        {

            string res = e.Result.ToString(); // make sure returned data is not Café’s login page.

            if (res.Contains("<?xml"))

                result = "Connected!";

            else

                result = "Not Connected!";

        }

 

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)

        {

            var t = (sender as WebClient).ResponseHeaders[HttpRequestHeader.ContentType];

 

            if (res.Contains("text/xml")) // make sure returned data is xml and not Café’s (html) login page.

                result = "Connected!";

            else

                result = "Not Connected!";

 

            }

        }

Thanks,

Mike