The HttpClient documentation and usage

William Thompson 120 Reputation points
2024-07-25T19:39:38.4566667+00:00

I am trying to use the HttpClient class in my code. I am using .NetFramework 4.7.2. When I look up how it can be used in my C# code, I go to https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.7.2

The problem is that the example on this page will not work with this version.

Please help.

Please reenact to verify. Look at the right class and version page and you will see.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,918 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 46,456 Reputation points Microsoft Vendor
    2024-07-26T08:50:50.6133333+00:00

    Hi @William Thompson , Welcome to Microsoft Q&A,

    In my try, it has no other problems except that using is applicable to c#8.0+.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace xxx
    {
        internal class Program
        {
            static readonly HttpClient client = new HttpClient();
            static async Task Main()
    {
        // Call asynchronous network methods in a try/catch block to handle exceptions.
        try
        {
              HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
            response.EnsureSuccessStatusCode();
            string responseBody = await response.Content.ReadAsStringAsync();
            // Above three lines can be replaced with new helper method below
            // string responseBody = await client.GetStringAsync(uri);
    
            Console.WriteLine(responseBody);
                    Console.ReadLine(  );
                }
        catch (HttpRequestException e)
        {
            Console.WriteLine("\nException Caught!");
            Console.WriteLine("Message :{0} ", e.Message);
    
        }
    }
            //static void Main(string[] args)
            //{
            //}
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. SurferOnWww 3,201 Reputation points
    2024-07-26T09:32:47.71+00:00

    The following using declaration used in the sample code in the HttpClient Class is introduced in C# version 8.0:

    using HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");
    

    Above declaration is not available in the project of .NET Framework 4.7.2 where its C# version is 7.3.

    Use previous expression as follow:

    using System;
    using System.Net.Http;
    using System.Threading.Tasks;
    
    namespace ConsoleAppNet472
    {
        internal class Program
        {
            // HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.
            static readonly HttpClient client = new HttpClient();
    
            static async Task Main(string[] args)
            {
                // Call asynchronous network methods in a try/catch block to handle exceptions.
                try
                {
                    using (HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/"))
                    {
                        response.EnsureSuccessStatusCode();
                        string responseBody = await response.Content.ReadAsStringAsync();
                        // Above three lines can be replaced with new helper method below
                        // string responseBody = await client.GetStringAsync(uri);
    
                        Console.WriteLine(responseBody);
                    }
                }
                catch (HttpRequestException e)
                {
                    Console.WriteLine("\nException Caught!");
                    Console.WriteLine("Message :{0} ", e.Message);
                }
            }
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.