How to use https in c#?

by scotty_walker , in category: Third Party Scripts , 3 months ago

How to use https in c#?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 3 months ago

@scotty_walker 

To use HTTPS in C#, you can use the HttpClient class from the System.Net.Http namespace. Here's an example of how you can make an HTTPS request using HttpClient:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        using (HttpClient client = new HttpClient())
        {
            HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/posts/1");

            if (response.IsSuccessStatusCode)
            {
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
            else
            {
                Console.WriteLine("Failed to make HTTPS request. Status code: " + response.StatusCode);
            }
        }
    }
}


In this example, we are making an HTTPS GET request to a dummy API (jsonplaceholder.typicode.com) and printing the response body if the request is successful. Make sure to handle exceptions and errors appropriately in your code when using HTTPS requests.