C# read from HttpResponseMessage
15,458
You are creating an asynchronous task but not waiting for it to complete before returning. This means your responseValue
never gets set.
To fix this, before your return do this:
task.Wait();
So your function now looks like this:
if (Method == HttpVerb.POST)
response = client.PostAsync(domain, new StringContent(parameters)).Result;
else
response = client.GetAsync(domain).Result;
if (response != null)
{
var responseValue = string.Empty;
Task task = response.Content.ReadAsStreamAsync().ContinueWith(t =>
{
var stream = t.Result;
using (var reader = new StreamReader(stream))
{
responseValue = reader.ReadToEnd();
}
});
task.Wait();
return responseValue;
}
If you prefer to use await
(which you possibly should), then you need to make the function this code is contained in async
. So this:
public string GetStuffFromSomewhere()
{
//Code above goes here
task.Wait();
}
Becomes:
public async string GetStuffFromSomewhere()
{
//Code above goes here
await ...
}
Author by
Haris
Updated on June 04, 2022Comments
-
Haris almost 2 years
I am using .net's Httpclient for the first time and finding it very hard. I have managed to call the server and receive response from it but stuck at reading from the response. Here is my code:
if (Method == HttpVerb.POST) response = client.PostAsync(domain, new StringContent(parameters)).Result; else response = client.GetAsync(domain).Result; if (response != null) { var responseValue = string.Empty; Task task = response.Content.ReadAsStreamAsync().ContinueWith(t => { var stream = t.Result; using (var reader = new StreamReader(stream)) { responseValue = reader.ReadToEnd(); } }); return responseValue; }
responseValue has {} in it although the service is returning data. How should I fix the issue?
The project is in .Net 4.
-
PrinceT almost 10 yearsyou are using wcf service or web service? What is the status of reponse? responseValue.StatusCode = ?
-
DavidG almost 10 yearsShouldn't you be
await
ing task before returning? -
Haris almost 10 years@PrinceT I am calling rest service
-
Haris almost 10 years@DavidG where should I use await? well where ever I put await VS shows error that The await operator can only be used within an async method
-
DavidG almost 10 years@Haris As your function is not async, just use
Task.Wait()
, check my answer.
-
-
DavidG almost 10 years
WebRequest
should not be used when the newerHttpClient
is available. Also, this doesn't answer the question being asked. -
Haris almost 10 yearsnope still the same :(. however it works perfectly fine with WebRequest but the problem due to which I had to move to HttpClient was that with WebRequest I was unable to read cookies.
-
Haris almost 10 yearsI was previously using WebRequest but I could not read cookies that is why moved to HttpClient. otherwise WebRequest works perfectly.
-
DavidG almost 10 yearsIs the function in the
ContinueWith
being executed? -
Haris almost 10 yearsyes it is being executed. noticed that t.Result.Length has 2 in it
-
DavidG almost 10 yearsI recommend reading this article to see how to use HttpClient: asp.net/web-api/overview/web-api-clients/…
-
Haris almost 10 yearsok I got the data by changing domain and parameters. Domain was changed from
http://mydomain.com:38080/workshop/
tohttp://mydomain.com:38080/workshop/rest/login
and parameters was changed from torest/login?username=usr&password=pwd
tousername=usr&password=pwd
. why is that? this will create problem of getting cookies as they are set at the domain level -
DavidG almost 10 yearsAh I see now. Well my answer still applies even though it's not a solution for you.
-
Haris almost 10 yearsthis means that I have to give complete address to rest controller. How will it affect the cookies (from where all this started). Because now when I get cookie from mydomain.com:38080/workshop I do not get anything