C# httpclient

C# httpclient

In this article, you will learn, how to send/receive data through, C# 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.

So, some example below links:-

C# HttpClient status code

 

HTTP response status codes provide whether a specific HTTP request has been successfully completed. Responses are grouped in five classes in following code:

 

  • Informational responses (100–199)
  • Successful responses (200–299)
  • Redirects (300–399)
  • Client errors (400–499)
  • Server errors (500–599)

 

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace usingcsharp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            using var client = new HttpClient();

            var results = await client.GetAsync("https://usingcsharp.com");
            Console.WriteLine(results.StatusCode);
        }
    }
}

 

C# HttpClient HEAD request

The HTTP HEAD method requests the headers that are returned if the specified resource would be requested with an HTTP GET method.

 

using System;
using System.Net.Http;

var url = "https://usingcsharp.com";
using var client = new HttpClient();
var results = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));
Console.WriteLine(results);

 

 

 

C# HttpClient GET request

 

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace usingcsharp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            using var client = new HttpClient();
            var contents = await client.GetStringAsync("http://usingcsharp.com");
            Console.WriteLine(contents);
        }
    }
}

 

It outputs the simple HTML code of the home page.

 

 

C# HttpClient status code

 

HTTP response status codes provide whether a specific HTTP request has been successfully completed. Responses are grouped in five classes in following code:

 

  • Informational responses (100–199)
  • Successful responses (200–299)
  • Redirects (300–399)
  • Client errors (400–499)
  • Server errors (500–599)

 

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace usingcsharp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            using var client = new HttpClient();

            var results = await client.GetAsync("https://usingcsharp.com");
            Console.WriteLine(results.StatusCode);
        }
    }
}

2021-10-03