Getting content/message from HttpResponseMessage
You need to call GetResponse().
Stream receiveStream = response.GetResponseStream ();
StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
txtBlock.Text = readStream.ReadToEnd();
How to read HttpResponseMessage content as text
The textual representation of the response is hidden in the Content
property of the HttpResponseMessage
class. Specifically, you get the response like this:
response.Content.ReadAsStringAsync();
Like all modern Async methods, ReadAsStringAsync
returns a Task
. To get the result directly, use the Result
property of the task:
response.Content.ReadAsStringAsync().Result;
Note that Result
is blocking. You can also await
ReadAsStringAsync()
.
Put content in HttpResponseMessage object?
Apparently the new way to do it is detailed here:
http://aspnetwebstack.codeplex.com/discussions/350492
To quote Henrik,
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new ObjectContent<T>(T, myFormatter, "application/some-format");
So basically, one has to create a ObjectContent type, which apparently can be returned as an HttpContent object.
Is there any way how to get HttpResponseMessage.Content body when response is HttpStatusCode.NoContent?
As @peinarydevelopment mentions in their comment, the httpclient
instance will not return a body for status codes that dont support one. The best solution to your problem is to fix the server (or ask whoever is responsible for it).
If you really must deal with the broken HTTP server, you could drop down to TCP level and retrieve the packets directly from the socket, put that seems like a very long way around the problem.
Getting content from HttpResponseMessage for testing using c# dynamic keyword
.ReadAsAsync<T>
is an asynchronous method, meaning that it doesn't return the whole deserialized object but a Task<T>
to handle the continuation of the whole asynchronous task.
You've two options:
1. Async pattern.
Use the async
keyword in your enclousing method (for example: public async void A()
) and do the asynchronous call this way:
dynamic responseContent = await response.Content.ReadAsAsync<object>();
string returnedToken = responseContent.Token;
2. Regular task API
Or just use the Task API:
response.Content.ReadAsAsync<object>().ContinueWith(task => {
// The Task.Result property holds the whole deserialized object
string returnedToken = ((dynamic)task.Result).Token;
});
It's up to you!
Update
Before you posted the whole screenshot, no one could know that you're calling task.Wait
in order to wait for the async result. But I'm going to maintain my answer because it may help further visitors :)
As I suggested in a comment to my own answer, you should try deserializing to ExpandoObject
. ASP.NET WebAPI uses JSON.NET as its underlying JSON serializer. That is, it can handle anonymous JavaScript object deserialization to expando objects.
Return Content From HttpResponseMessage and Map to POCO
You can use DeserializeObject
method from Newtonsoft.Json.JsonConvert
class like this:
var response = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<TeamName>>(response);
Finding result of POST in Http Response Message
Why not trying to use HttpContent
like below:
using (HttpResponseMessage response = await client.GetAsync(url)) // here you can use your own implementation i.e PostAsJsonAsync
{
using (HttpContent content = response.Content)
{
string responseFromServer = await content.ReadAsStringAsync();
}
}
Related Topics
Forms' Does Not Exist in the Namespace System.Windows
How to Render an ASP.NET MVC View in PDF Format
Proper Datagrid Search from Textbox in Wpf Using Mvvm
Current Possibilities for Tracing Program Flow in C#
Ensure That Httpconfiguration.Ensureinitialized()
Linq Syntax - Selecting Multiple Columns
How to Partially Project a Child Object with Many Fields in Nhibernate
How to Check If a Class Inherits Another Class Without Instantiating It
Using C# to Authenticate User Against Ldap
How to Derive Xml Element Name from an Attribute Value of a Class Using Annotations
How to Merge Two Lists Using Linq
How to Setup Multiple Auth Schemes in ASP.NET Core 2.0