It is common that REST APIs return useful information for developers in the response content when the request is not successful.

For example, lets say you want to access the following URL from the Facebook API:

http://graph.facebook.com/v2.2/803370993032579

But you forget to set the authentication parameters in the request, so you make a request to the URL without setting the OAuth token. By inspecting DownloadOperation.Progress.Status, you see the response was 400 Bad Request. But that is not enough information to fix the problem!

The actual Facebook server response contains more information, look at the raw HTTP response:

Access-Control-Allow-Origin: *
Cache-Control: no-store
Connection: keep-alive
Content-Length: 146
Content-Type: application/json; charset=UTF-8
Date: Tue, 04 Nov 2014 03:25:14 GMT
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Facebook-API-Version: v2.2
Pragma: no-cache
WWW-Authenticate: OAuth "Facebook Platform" "invalid_token" "An access token is required to request this resource."
X-FB-Debug: MUJRrIP4wQYZWjXwzGkjsvt8+QaLFJlfAX3w9CRaAXdcSQt5Zs6X8/bd1zWfQzMQBu60XbBF//LpRFR3i2Ejwg==
X-FB-Rev: 1479988

{
  "error": {
    "message": "An access token is required to request this resource.",
    "type": "OAuthException",
    "code": 104
  }
}

You can access the rest of the error information using DownloadOperation.GetResultStreamAt() or UploadOperation.GetResultStreamAt(). You have to get the IInputStream, create a DataReader, load the DataReader and finally read the response content as string.

Here is the example:

// using Windows.Networking.BackgroundTransfer
// using System.Diagnostics;

IStorageFile resultFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
    "blah.txt",
    CreationCollisionOption.ReplaceExisting);
BackgroundDownloader downloader = new BackgroundDownloader();
Uri uri = new Uri("http://graph.facebook.com/v2.2/803370993032579");
DownloadOperation download = downloader.CreateDownload(uri, resultFile);

await download.StartAsync();

Debug.WriteLine(download.Progress.Status); // 400 Bad Request

IInputStream inputStream = download.GetResultStreamAt(0);
DataReader reader = new DataReader(inputStream);
reader.InputStreamOptions = InputStreamOptions.Partial;

var bytesLoaded = await reader.LoadAsync(1000000);

// Useful error information:
Debug.WriteLine(reader.ReadString(reader.UnconsumedBufferLength));