CHAPTER 9 EXERCISE SOLUTIONS

Exercise 1 Solution

The valid HTTP methods (verbs) in the list are GET, DELETE, POST, and PUT (a, c, d, and e). ZAP isn't allowed, and EXSANGUINATE is just plain silly.

Exercise 2 Solution

You use the UploadStringAsync() method of the WebClient class and respond to the UploadStringCompleted event.

Exercise 3 Solution

The following call will fail:

ResultTextBox.Text = sr.ReadToEnd();

If you access the user interface, you must use Dispatcher, as in this example:

string text = sr.ReadToEnd();
Dispatcher.BeginInvoke(new Action(() => ResultTextBox.Text = text));

Exercise 4 Solution

Recall that the Flickr API error message format is as follows:

<?xml version=“1.0” encoding=“utf-8” ?>
<rsp stat=“fail”>
  <err code=“[error-code]” msg=“[error-message]” />
</rsp>

One way to achieve the desired result and extract this information in case of error is to modify the GetUserRequestCallback() method as follows:

private void GetUserRequestCallback(IAsyncResult result) { HttpWebResponse response = (result.AsyncState as HttpWebRequest) .EndGetResponse(result) as HttpWebResponse; XDocument resultXml = XDocument.Load(response.GetResponseStream()); string status = resultXml.Root.Attribute(“stat”).Value; if (status == “ok”) { string userNsid = (from user in resultXml.Descendants(“user”) select user.Attribute(“nsid”).Value) .FirstOrDefault(); HttpWebRequest getPhotosRequest = WebRequest.CreateHttp( flickrUriHelper.GetUri( “flickr.people.getPublicPhotos”, new Dictionary<string, ...

Get Beginning Windows® Phone 7 Application Development: Building Windows® Phone Applications Using Silverlight® and XNA® now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.