Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i wait until the response is returned before returning in http request c#?

I want to wait for the response to be returned before returning the result:

     private static async Task<object> GetTestFuncAppAsync()
    {
        var result = string.Empty;
        using (HttpClient client = new HttpClient())
        {                
            string requestUrl = <myurl>
            HttpResponseMessage response = await client.GetAsync(requestUrl);             
            if (response.IsSuccessStatusCode)
            {
                string responseContent = await response.Content.ReadAsStringAsync();
                result = responseContent;                 
            }
            else
            {
            }
        }
        return result;
    }

How can i wait until the responseContent is read before return result ?
What seems to be happening is the it calls client.GetAsync and the calls return result and there is garbage text in there .
How can i wait until the response is finished ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Looks like there shouldn’t be any problems since you’re already using async. Maybe try returning Task<string> instead of Task<object>? Maybe it’s serializing weirdly in translation. What’s the http response code?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading