How to create a http client in C#
- User
How to create a http client in C#
- Assistant
To create an HTTP client in C#, you can use the
HttpClientclass from theSystem.Net.Httpnamespace. Here's an example of how to create an HTTP client:using System; using System.Net.Http; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { // Create an instance of HttpClient using (HttpClient client = new HttpClient()) { try { // Send a GET request to a URL HttpResponseMessage response = await client.GetAsync("https://example.com"); // Check if the request was successful if (response.IsSuccessStatusCode) { // Read the response content as a string string content = await response.Content.ReadAsStringAsync(); Console.WriteLine(content); } else { Console.WriteLine("Request failed with status code: " + response.StatusCode); } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } } }