Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use HttpClient without async

Hello I'm following to this guide

static async Task<Product> GetProductAsync(string path)
{
    Product product = null;
    HttpResponseMessage response = await client.GetAsync(path);
    if (response.IsSuccessStatusCode)
    {
        product = await response.Content.ReadAsAsync<Product>();
    }
    return product;
}

I use this example on my code and I want to know is there any way to use HttpClient without async/await and how can I get only string of response?

Thank you in advance

like image 741
Kumar J. Avatar asked Oct 23 '16 22:10

Kumar J.


People also ask

Are all methods with httpclient asynchronous?

All methods with HttpClient are asynchronous. HttpClient class provides a base class for sending/receiving the HTTP requests/responses from a URL. It is a supported async feature of .NET framework. HttpClient is able to process multiple concurrent requests. It is a layer over HttpWebRequest and HttpWebResponse.

Are You using httpclient in the right way?

Are You Using HttpClient in The Right Way? When an ASP NET application needs to talk to an external service or API, it needs to make an HTTP Request. When using ASP.NET to build an application, HTTP requests is made using an instance of the HttpClient class. An HttpClient class acts as a session to send HTTP Requests.

Why httpclient does not support deleteasync method?

IsSuccessStatusCode) { Console.WriteLine("Success"); } HttpClient only supports DeleteAsync method because Delete method does not have a request body. HttpClient is used to send an HTTP request, using a URL.

What is postasjsonasync method in httpclient?

In this code, PostAsJsonAsync method serializes the object into JSON format and sends this JSON object in POST request. HttpClient has a built-in method "PostAsXmlAsync" to send XML in POST request. Also, we can use "PostAsync" for any other formatter.


2 Answers

Of course you can:

public static string Method(string path)
{
   using (var client = new HttpClient())
   {
       var response = client.GetAsync(path).GetAwaiter().GetResult();
       if (response.IsSuccessStatusCode)
       {
            var responseContent = response.Content;
            return responseContent.ReadAsStringAsync().GetAwaiter().GetResult();
        }
    }
 }

but as @MarcinJuraszek said:

"That may cause deadlocks in ASP.NET and WinForms. Using .Result or .Wait() with TPL should be done with caution".

Here is the example with WebClient.DownloadString

using (var client = new WebClient())
{
    string response = client.DownloadString(path);
    if (!string.IsNullOrEmpty(response))
    {
       ...
    }
}
like image 167
Roman Marusyk Avatar answered Oct 18 '22 05:10

Roman Marusyk


is there any way to use HttpClient without async/await and how can I get only string of response?

HttpClient was specifically designed for asynchronous use.

If you want to synchronously download a string, use WebClient.DownloadString.

like image 29
Stephen Cleary Avatar answered Oct 18 '22 04:10

Stephen Cleary