I'm trying to get content of HttpResponseMessage. It should be: {"message":"Action '' does not exist!","success":false}
, but I don't know, how to get it out of HttpResponseMessage.
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync("http://****?action=");
txtBlock.Text = Convert.ToString(response); //wrong!
In this case txtBlock would have value:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Vary: Accept-Encoding
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Date: Wed, 10 Apr 2013 20:46:37 GMT
Server: Apache/2.2.16
Server: (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze14
Content-Length: 55
Content-Type: text/html
}
Try this, you can create an extension method like this:
and then, simple call the extension method:
I hope this help you ;-)
If you want to cast it to specific type (e.g. within tests) you can use ReadAsAsync extension method:
or following for synchronous code:
Update: there is also generic option of ReadAsAsync<> which returns specific type instance instead of object-declared one:
By the answer of rudivonstaden
but if you don't want to make the method async you can use
Wait() it's important, becаuse we are doing async operations and we must wait for the task to complete before going ahead.
You can use the
GetStringAsync
method:You need to call GetResponse().
I think the easiest approach is just to change the last line to
This way you don't need to introduce any stream readers and you don't need any extension methods.