Why use HttpClient for Synchronous Connection Why use HttpClient for Synchronous Connection asp.net asp.net

Why use HttpClient for Synchronous Connection


but what i am doing is purely synchronous

You could use HttpClient for synchronous requests just fine:

using (var client = new HttpClient()){    var response = client.GetAsync("http://google.com").Result;    if (response.IsSuccessStatusCode)    {        var responseContent = response.Content;         // by calling .Result you are synchronously reading the result        string responseString = responseContent.ReadAsStringAsync().Result;        Console.WriteLine(responseString);    }}

As far as why you should use HttpClient over WebRequest is concerned, well, HttpClient is the new kid on the block and could contain improvements over the old client.


I'd re-iterate Donny V. answer and Josh's

"The only reason I wouldn't use the async version is if I were trying to support an older version of .NET that does not already have built in async support."

(and upvote if I had the reputation.)

I can't remember the last time if ever, I was grateful of the fact HttpWebRequest threw exceptions for status codes >= 400. To get around these issues you need to catch the exceptions immediately, and map them to some non-exception response mechanisms in your code...boring, tedious and error prone in itself. Whether it be communicating with a database, or implementing a bespoke web proxy, its 'nearly' always desirable that the Http driver just tell your application code what was returned, and leave it up to you to decide how to behave.

Hence HttpClient is preferable.


public static class AsyncHelper  {    private static readonly TaskFactory _taskFactory = new        TaskFactory(CancellationToken.None,                    TaskCreationOptions.None,                    TaskContinuationOptions.None,                    TaskScheduler.Default);    public static TResult RunSync<TResult>(Func<Task<TResult>> func)        => _taskFactory            .StartNew(func)            .Unwrap()            .GetAwaiter()            .GetResult();    public static void RunSync(Func<Task> func)        => _taskFactory            .StartNew(func)            .Unwrap()            .GetAwaiter()            .GetResult();}

Then

AsyncHelper.RunSync(() => DoAsyncStuff());

if you use that class pass your async method as parameter you can call the async methods from sync methods in a safe way.

it's explained here :https://cpratt.co/async-tips-tricks/