是的,C#可以支持HTTPS請(qǐng)求。在C#中,可以使用HttpClient類來(lái)發(fā)送HTTPS請(qǐng)求??梢酝ㄟ^(guò)在HttpClient實(shí)例上設(shè)置HttpClientHandler的屬性來(lái)配置SSL選項(xiàng),以確保安全連接。以下是一個(gè)簡(jiǎn)單的示例代碼:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.example.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("endpoint");
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine("Error: " + response.StatusCode);
}
}
}
}
在上面的例子中,我們創(chuàng)建了一個(gè)HttpClient實(shí)例,并設(shè)置了基本地址和Accept標(biāo)頭。然后,我們使用GetAsync方法發(fā)送HTTPS請(qǐng)求,并處理響應(yīng)。通過(guò)這種方式,就可以在C#中發(fā)送HTTPS請(qǐng)求。