13

I'm trying to download a JSON string in my Windows Store App which should look like this:

{
 "status": "okay",
 "result": {"id":"1",
            "type":"monument",
            "description":"The Spire",
            "latitude":"53.34978",
            "longitude":"-6.260316",
            "private": "{\"tag\":\"david\"}"}
}

but I'm getting what looks like info about the server. The output I'm getting is as follows:

Response: StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  MS-Author-Via: DAV
  Keep-Alive: timeout=15, max=100
  Connection: Keep-Alive
  Date: Thu, 22 Nov 2012 15:13:53 GMT
  Server: Apache/2.2.22
  Server: (Unix)
  Server: DAV/2
  Server: PHP/5.3.15
  Server: with
  Server: Suhosin-Patch
  Server: mod_ssl/2.2.22
  Server: OpenSSL/0.9.8r
  X-Powered-By: PHP/5.3.15
  Content-Length: 159
  Content-Type: text/json
}

I've been looking around and see that WebClient was used before Windows 8, and is now replaced with HttpClient. So instead of using DownloadString(), I've been using Content.ReadAsString(). Here's the bit of code I have so far:

public async Task<string> GetjsonStream()
{
    HttpClient client = new HttpClient();
    string url = "http://(urlHere)";
    HttpResponseMessage response = await client.GetAsync(url);
    Debug.WriteLine("Response: " + response);
    return await response.Content.ReadAsStringAsync();
}

Anyone know where I'm going wrong? Thanks in advance!

1

2 Answers 2

24

You are outputting the server response. The server response contains a StreamContent (see documentation here) but this StreamContent doesn't define a ToString, so the class name is output instead of the content.

ReadAsStringAsync (documentation here) is the right method to get the content sent back by the server. You should print out the return value of this call instead:

public async Task<string> GetjsonStream()
{
    HttpClient client = new HttpClient();
    string url = "http://(urlHere)";
    HttpResponseMessage response = await client.GetAsync(url);
    string content = await response.Content.ReadAsStringAsync();
    Debug.WriteLine("Content: " + content);
    return content;
}
1
  • Thanks emartel. I understand now. I didn't have the actual content in a string and the line you added in provides a way for it to output the string correctly.. well I think that's what you mean. :) Saved me a lot of pain and head aches though! Thanks again! Commented Nov 22, 2012 at 16:25
0

If you are inside await block you might need to get the Result ReadAsStringAsync().Result.

Example:

public async Task<HttpResponseMessage> Listen()
{
   await Request.Content.ReadAsMultipartAsync<MultipartMemoryStreamProvider>(
      new MultipartMemoryStreamProvider()).ContinueWith(task =>
      {
         MultipartMemoryStreamProvider multipartMemoryStreamProvider = task.Result;

         var imageContent = multipartMemoryStreamProvider.Contents.First();
         string name = imageContent.Headers.ContentDisposition.Name;
         string fileName = imageContent.Headers.ContentDisposition.FileName;
         data = imageContent.ReadAsByteArrayAsync().Result;

         string content = multipartMemoryStreamProvider.Contents.Last().ReadAsStringAsync().Result;

         model = multipartMemoryStreamProvider.Contents.Last().ReadAsAsync<RecordingModel>().Result;
      }
   );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.