Handling Authenticated Zip files in Windows Phone

This will be a short post on a little roadblock that was overcome.

To-Do:

An internal Windows Phone application that we are building for Sogeti needed to pull down a chunk of data from a server for caching & displaying on the phone.

Meh .. So, what’s the Challenge?

Well, two little issues:

  • The content was sitting behind a Basic Auth protection with Windows credentials.
  • The downloadable piece is a Zipped file with hierarchical content, that had to be unwrapped.

Problems with the usual suspects

  • For some reason, WebClient refuses to honor Header information attached, which contains the user’s credentials to clear the basic authentication. Yet to figure out why .. may be Fiddler would help.
  • The Zipped file was a red flag. Although pulled down as HTTPResponse, how do we unpack & parse content files?

The Solution

Turns out, the solution was quite simple & included right in the toolset itself. As we know, WebClient is an abstraction over HttpWebRequest and big brother does do a few things better, including honoring HTTP Header information correctly. And after looking around for 3rd party libraries to unzip the content, good ol’ .NET seemed sufficient to reach inside the zipped content to parse exactly what’s needed. Here’s some sample code:

                      
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://someURI/File.zip"));

 byte[] credentialBuffer = new UTF8Encoding().GetBytes("UserID" + ":" + "Password");
 request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);

 request.BeginGetResponse(r =>
 {
    var httpRequest = (HttpWebRequest)r.AsyncState;
    var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r);

    using (var reader = new StreamReader(httpResponse.GetResponseStream()))
    {
        StreamResourceInfo info = new StreamResourceInfo(httpResponse.GetResponseStream(), string.Empty);
        StreamResourceInfo fileStreamInfo = App.GetResourceStream(info, new Uri("SomeFolder/SomeFile.txt", UriKind.Relative));

        StreamReader fileReader = new StreamReader(fileStreamInfo.Stream);
        string rawText = fileReader.ReadToEnd();

        Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
        {
           // Update UI with rawText.
        }));
     }
 }, request);

As you could see, we reached inside the hierarchical folder structure within the Zipped file & were able to pull out file content. And since this is read in as a byte stream, the content could be any type of file that could be read from the Windows Phone client. Voila!

Now, this technique has one weakness – you need to know the folder structure inside the Zipped file to pull out exactly the content you want. If your goal is to explore the unknown inside a Zipped file, this would obviously not work. Two solutions I came across for such scenarios are Silverlight SharpZipLib and SharpGIS Unzipper.

Hope this was helpful.

Adios!

Advertisement

One thought on “Handling Authenticated Zip files in Windows Phone

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 )

Facebook photo

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

Connecting to %s