Mobile Zone is brought to you in partnership with:

Husband, Sr Trainer with @Telerik, Windows/Web Developer, @INETA Secretary, @WindowsDevUG Leader, @M3Conf Secretary, Speaker, Author, Gadget-lover. Yes, a geek! Samidip is a DZone MVB and is not an employee of DZone and has posted 17 posts at DZone. You can read more from them at their website. View Full User Profile

Handling Authenticated Zip Files in Windows Phone

03.30.2012
| 2425 views |
  • submit to reddit

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!

Published at DZone with permission of Samidip Basu, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)