using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// 创建HttpClient实例
using (HttpClient client = new HttpClient())
{
try
{
// 发送GET请求并获取响应
HttpResponseMessage response = await client.GetAsync("https://api.example.com/data");
// 确保请求成功
response.EnsureSuccessStatusCode();
// 读取响应内容
string responseBody = await response.Content.ReadAsStringAsync();
// 输出响应内容
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
// 捕获异常并输出错误信息
Console.WriteLine($"请求错误: {e.Message}");
}
}
}
}
上一篇:c# 序列化和反序列化
下一篇:c# 注释
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站