using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
try
{
// 发送GET请求并获取响应
HttpResponseMessage response = await client.GetAsync("https://api.example.com/data");
response.EnsureSuccessStatusCode(); // 确保状态码为200-299
// 读取响应内容
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine($"请求错误: {e.Message}");
}
}
}
}
HttpClient
资源。如果你需要更多示例或有其他问题,请告诉我!
上一篇:c# using()
下一篇:c# 定义常量
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站