Using Cloud Storage for Your Windows Phone 7 (WP7) App

The ability to communicate with cloud services is a necessary capability for
many applications on Windows Phone 7
(WP7). Yes, sometimes you will need to create your own cloud services to suit
the needs of your app; and if you need a create your own cloud service, you can
utilize such services as Microsoft
Azure platform
, Amazon Web Services,
etc. However, creating your own cloud service can be quite expensive both in
terms of development and hosting. While this may be unavoidable, many apps can
get away with a far simpler approach to storage.

Dropbox and Box.Net,
as well as other similar services provide free online storage to users. Dropbox
and Box.Net services are free of charge and include several gigabytes of
storage. Conveniently enough, most of these services provide an API which is
accessible through several different protocols including Web Services/SOAP, Rest Services, Web DAV, etc. While it
would be handy to have a set native library, it most likely is not going to
happen since WP7 is still a relatively new platform. However, you can still
access these services through either an open source library or you can directly
make calls to the API yourself.

Web Method Calls

The recommended method for making web requests is to use HttpWebRequest
in the System.Net namespace. Using the HttpWebRequest, it is easy to call
methods that require the use of HTTP GET or POST. The process for the two is
slightly different and we will start with the simpler HTTP GET as shown below.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
request.Method = "GET";

request.BeginGetResponse(OnResponseReceived, request);

The above code snippet first creates a new HttpWebRequest and sets the
method to GET. The BeginGetResponse starts the asynchronous process to get the
response from the request. The BeginGetResponse accepts two parameters, the
method to fire when complete, as well as a state object. In this case we are
passing in the request object itself to make it easy for processing downstream.
Once the GET request completes, the OnResponseReceived method will be triggered
to allow for processing of the returned data. A simple method that illustrates
the GET response is shown below.

void OnResponseReceived(IAsyncResult result)
{
HttpWebRequest req = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)req.EndGetResponse(result);
Stream s = response.GetResponseStream();

using (StreamReader sr = new StreamReader(s))
{
System.Diagnostics.Debug.WriteLine(sr.ReadToEnd());
}
response.Dispose();
}

The OnResponseReceived method accepts a single parameter of type
IAsyncResult, which includes the state object passed in the BeginGetResponse.
After getting access to the HttpWebRequest we need to call EndGetResponse to
prevent future calls to the OnResponseReceived method. Next we need to call
GetResponseStream, which returns a stream with all of the data returned from
the request.

To change the code around to do an HTTP POST instead of a GET is fairly
simply; first we change the code around to start the request as follows:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(/* Your URL */);
request.Method = "POST";

request.BeginGetRequestStream(OnRequestReceived, request);

As you can see, the method has been changed from GET to POST and the method that
we are calling to start the process has been changed to BeginGetRequestStream.
Similar to the BeginGetResponse, BeginGetRequestStream accepts two parameters:
a method to be called and a state object. The OnRequestReceived example is
shown below.

void OnRequestReceived(IAsyncResult result)
{
HttpWebRequest req = (HttpWebRequest)result.AsyncState;
Stream s = req.EndGetRequestStream(result);
//Write data to the stream if you have data to post
s.Close();

req.BeginGetResponse(OnResponseReceived, req);
}

As you can see, we first need to get access to the HttpWebRequest; end the
asynchronous process by calling EndGetRequestStream. The EndGetRequestStream
returns a Stream that is used to send data needed to post as part of the POST
method. Finally we call BeginGetResponse, which will then trigger the
background process to get the response from the POST. Once the POST process is
finished it will again call the OnResponseReceived method as shown above.

Conclusion

Using the HttpWebRequest object is a simple process for calling web
methods. One method I did not mention above is the ability to call traditional
WebServices using the SOAP protocol and while it is possible, it is not
recommended. WebServices provide quite a bit of overhead, which can be
eliminated by instead going with REST type services which use XML or JSON
(JavaScript Object Notation) as a payload. Generally speaking, REST services
will be faster (from a data transfer perspective), since the amount of data transfered
is quite a bit small. Using the methods above you should find it fairly easy to
integrate with the various online Cloud Services for your application.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read