Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between HttpClient.GetStringAsync and WebClient.DownloadStringAsync

I have the following code

static void Main(string[] args)
{
        string url = "http://www.google.com";
        Console.WriteLine(GetUrl(url).Result); // throws TaskCanceledException
        Console.WriteLine(GetUrl2(url).Result);
    }

    public static Task<string> GetUrl(string url)
    {
        using (var client = new HttpClient())
        {
            return client.GetStringAsync(url);
        }
    }

    public static Task<string> GetUrl2(string url)
    {
        using (var client = new WebClient())
        {
            return client.DownloadStringTaskAsync(url);
        }
    }

I'm trying to get the string of an url, the problem is GetUrl method (uses HttpClient's GetStringAsync) throws an TaskCacelledException, but GetUrl2 method (uses WebClient's DownloadStringTaskAsync) runs correctly. Is this caused due to using statement? What am I missing?

Edit. In this example I'm calling Result on the task because this is a console application, I know that it is best to await the result in a event handler for example.

like image 531
enrique7mc Avatar asked Dec 19 '14 17:12

enrique7mc


People also ask

What is the difference between HttpClient and WebClient?

In a nutshell, WebRequest—in its HTTP-specific implementation, HttpWebRequest—represents the original way to consume HTTP requests in . NET Framework. WebClient provides a simple but limited wrapper around HttpWebRequest. And HttpClient is the new and improved way of doing HTTP requests and posts, having arrived with .

Is there an alternative to WebClient?

NET 4.5 platform the community developed an alternative. Today, RestSharp is one of the only options for a portable, multi-platform, unencumbered, fully open-source HTTP client that you can use in all of your applications. It combines the control of HttpWebRequest with the simplicity of WebClient .

What is GetStringAsync Method?

GetStringAsync(String, CancellationToken) Send a GET request to the specified Uri and return the response body as a string in an asynchronous operation. GetStringAsync(Uri, CancellationToken) Send a GET request to the specified Uri and return the response body as a string in an asynchronous operation.


1 Answers

Is this caused due to using statement?

Yes. In both code examples, you're disposing the underlying client before the operation completes. Both code examples should be changed as such:

public static async Task<string> GetUrlAsync(string url)
{
    using (var client = new HttpClient())
    {
        return await client.GetStringAsync(url);
    }
}

public static async Task<string> GetUrl2Async(string url)
{
    using (var client = new WebClient())
    {
        return await client.DownloadStringTaskAsync(url);
    }
}

The behavior of asynchronous downloads when their underlying clients are disposed is undocumented. It's best not to dispose the clients until your code is done using them.

like image 65
Stephen Cleary Avatar answered Sep 26 '22 09:09

Stephen Cleary